Upgrade Ava to v4
This commit is contained in:
parent
9a40cc5274
commit
ce89f1b611
1153 changed files with 27264 additions and 95308 deletions
123
node_modules/ava/lib/serialize-error.js
generated
vendored
123
node_modules/ava/lib/serialize-error.js
generated
vendored
|
|
@ -1,42 +1,45 @@
|
|||
'use strict';
|
||||
const path = require('path');
|
||||
const cleanYamlObject = require('clean-yaml-object');
|
||||
const concordance = require('concordance');
|
||||
const isError = require('is-error');
|
||||
const slash = require('slash');
|
||||
const StackUtils = require('stack-utils');
|
||||
const assert = require('./assert');
|
||||
const concordanceOptions = require('./concordance-options').default;
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import {fileURLToPath, pathToFileURL} from 'node:url';
|
||||
|
||||
import cleanYamlObject from 'clean-yaml-object';
|
||||
import concordance from 'concordance';
|
||||
import isError from 'is-error';
|
||||
import StackUtils from 'stack-utils';
|
||||
|
||||
import {AssertionError} from './assert.js';
|
||||
import concordanceOptions from './concordance-options.js';
|
||||
|
||||
function isAvaAssertionError(source) {
|
||||
return source instanceof assert.AssertionError;
|
||||
return source instanceof AssertionError;
|
||||
}
|
||||
|
||||
function filter(propertyName, isRoot) {
|
||||
return !isRoot || (propertyName !== 'message' && propertyName !== 'name' && propertyName !== 'stack');
|
||||
}
|
||||
|
||||
function normalizeFile(file, ...base) {
|
||||
return file.startsWith('file://') ? file : pathToFileURL(path.resolve(...base, file)).toString();
|
||||
}
|
||||
|
||||
const stackUtils = new StackUtils();
|
||||
function extractSource(stack, testFile) {
|
||||
if (!stack || !testFile) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Normalize the test file so it matches `callSite.file`.
|
||||
const relFile = path.relative(process.cwd(), testFile);
|
||||
const normalizedFile = process.platform === 'win32' ? slash(relFile) : relFile;
|
||||
testFile = normalizeFile(testFile);
|
||||
|
||||
for (const line of stack.split('\n')) {
|
||||
try {
|
||||
const callSite = stackUtils.parseLine(line);
|
||||
if (callSite.file === normalizedFile) {
|
||||
return {
|
||||
isDependency: false,
|
||||
isWithinProject: true,
|
||||
file: path.resolve(process.cwd(), callSite.file),
|
||||
line: callSite.line
|
||||
};
|
||||
}
|
||||
} catch {}
|
||||
const callSite = stackUtils.parseLine(line);
|
||||
if (callSite && normalizeFile(callSite.file) === testFile) {
|
||||
return {
|
||||
isDependency: false,
|
||||
isWithinProject: true,
|
||||
file: testFile,
|
||||
line: callSite.line,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
@ -52,8 +55,8 @@ function buildSource(source) {
|
|||
// directory set to the project directory.
|
||||
const projectDir = process.cwd();
|
||||
|
||||
const file = path.resolve(projectDir, source.file.trim());
|
||||
const rel = path.relative(projectDir, file);
|
||||
const file = normalizeFile(source.file.trim(), projectDir);
|
||||
const rel = path.relative(projectDir, fileURLToPath(file));
|
||||
|
||||
const [segment] = rel.split(path.sep);
|
||||
const isWithinProject = segment !== '..' && (process.platform !== 'win32' || !segment.includes(':'));
|
||||
|
|
@ -63,60 +66,59 @@ function buildSource(source) {
|
|||
isDependency,
|
||||
isWithinProject,
|
||||
file,
|
||||
line: source.line
|
||||
line: source.line,
|
||||
};
|
||||
}
|
||||
|
||||
function trySerializeError(err, shouldBeautifyStack, testFile) {
|
||||
const stack = err.savedError ? err.savedError.stack : err.stack;
|
||||
function trySerializeError(error, shouldBeautifyStack, testFile) {
|
||||
const stack = error.savedError ? error.savedError.stack : error.stack;
|
||||
|
||||
const retval = {
|
||||
avaAssertionError: isAvaAssertionError(err),
|
||||
avaAssertionError: isAvaAssertionError(error),
|
||||
nonErrorObject: false,
|
||||
source: extractSource(stack, testFile),
|
||||
stack,
|
||||
shouldBeautifyStack
|
||||
shouldBeautifyStack,
|
||||
};
|
||||
|
||||
if (err.actualStack) {
|
||||
retval.stack = err.actualStack;
|
||||
if (error.actualStack) {
|
||||
retval.stack = error.actualStack;
|
||||
}
|
||||
|
||||
if (retval.avaAssertionError) {
|
||||
retval.improperUsage = err.improperUsage;
|
||||
retval.message = err.message;
|
||||
retval.name = err.name;
|
||||
retval.statements = err.statements;
|
||||
retval.values = err.values;
|
||||
retval.improperUsage = error.improperUsage;
|
||||
retval.message = error.message;
|
||||
retval.name = error.name;
|
||||
retval.values = error.values;
|
||||
|
||||
if (err.fixedSource) {
|
||||
const source = buildSource(err.fixedSource);
|
||||
if (error.fixedSource) {
|
||||
const source = buildSource(error.fixedSource);
|
||||
if (source) {
|
||||
retval.source = source;
|
||||
}
|
||||
}
|
||||
|
||||
if (err.assertion) {
|
||||
retval.assertion = err.assertion;
|
||||
if (error.assertion) {
|
||||
retval.assertion = error.assertion;
|
||||
}
|
||||
|
||||
if (err.operator) {
|
||||
retval.operator = err.operator;
|
||||
if (error.operator) {
|
||||
retval.operator = error.operator;
|
||||
}
|
||||
} else {
|
||||
retval.object = cleanYamlObject(err, filter); // Cleanly copy non-standard properties
|
||||
if (typeof err.message === 'string') {
|
||||
retval.message = err.message;
|
||||
retval.object = cleanYamlObject(error, filter); // Cleanly copy non-standard properties
|
||||
if (typeof error.message === 'string') {
|
||||
retval.message = error.message;
|
||||
}
|
||||
|
||||
if (typeof err.name === 'string') {
|
||||
retval.name = err.name;
|
||||
if (typeof error.name === 'string') {
|
||||
retval.name = error.name;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof err.stack === 'string') {
|
||||
const lines = err.stack.split('\n');
|
||||
if (err.name === 'SyntaxError' && !lines[0].startsWith('SyntaxError')) {
|
||||
if (typeof error.stack === 'string') {
|
||||
const lines = error.stack.split('\n');
|
||||
if (error.name === 'SyntaxError' && !lines[0].startsWith('SyntaxError')) {
|
||||
retval.summary = '';
|
||||
for (const line of lines) {
|
||||
retval.summary += line + '\n';
|
||||
|
|
@ -127,11 +129,8 @@ function trySerializeError(err, shouldBeautifyStack, testFile) {
|
|||
|
||||
retval.summary = retval.summary.trim();
|
||||
} else {
|
||||
// Skip the source line header inserted by `esm`:
|
||||
// <https://github.com/standard-things/esm/wiki/improved-errors>
|
||||
const start = lines.findIndex(line => !/:\d+$/.test(line));
|
||||
retval.summary = '';
|
||||
for (let index = start; index < lines.length; index++) {
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
if (lines[index].startsWith(' at')) {
|
||||
break;
|
||||
}
|
||||
|
|
@ -146,17 +145,17 @@ function trySerializeError(err, shouldBeautifyStack, testFile) {
|
|||
return retval;
|
||||
}
|
||||
|
||||
function serializeError(origin, shouldBeautifyStack, err, testFile) {
|
||||
if (!isError(err)) {
|
||||
export default function serializeError(origin, shouldBeautifyStack, error, testFile) {
|
||||
if (!isError(error)) {
|
||||
return {
|
||||
avaAssertionError: false,
|
||||
nonErrorObject: true,
|
||||
formatted: concordance.formatDescriptor(concordance.describe(err, concordanceOptions), concordanceOptions)
|
||||
formatted: concordance.formatDescriptor(concordance.describe(error, concordanceOptions), concordanceOptions),
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
return trySerializeError(err, shouldBeautifyStack, testFile);
|
||||
return trySerializeError(error, shouldBeautifyStack, testFile);
|
||||
} catch {
|
||||
const replacement = new Error(`${origin}: Could not serialize error`);
|
||||
return {
|
||||
|
|
@ -165,9 +164,7 @@ function serializeError(origin, shouldBeautifyStack, err, testFile) {
|
|||
name: replacement.name,
|
||||
message: replacement.message,
|
||||
stack: replacement.stack,
|
||||
summary: replacement.message
|
||||
summary: replacement.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = serializeError;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue