@tapjs/node-serialize
Version:
Stream TAP test data as a serialized node:test stream
39 lines • 1.48 kB
JavaScript
// turn a tap-parser Result object into an Error
// Put some of the diagnostic info into the error message,
// because that is the string the node --test reporter shows.
import { expandStack, } from '@tapjs/stack';
import { locFromCallSite } from './loc-from-callsite.js';
import { prettyDiff } from './pretty-diff.js';
export const resultToError = (result, test) => {
const { stack: diagStack, diff: rawDiff, source, passes: _, ...diag } = result.diag || {};
const at = diag.at ?? test?.options.at;
const lfa = !!at ? locFromCallSite(at) : undefined;
const stack = diagStack ||
(lfa?.file ? `${lfa.file}:${lfa.line}:${lfa.column}` : undefined);
delete diag.at;
const diff = prettyDiff(rawDiff);
if (diff) {
delete diag.actual;
delete diag.expected;
delete diag.found;
delete diag.wanted;
}
else if (result.diag) {
if ('found' in diag) {
diag.actual = diag.found;
}
if ('wanted' in diag) {
diag.expected = diag.wanted;
}
delete diag.found;
delete diag.wanted;
}
const name = (result.name || result.tapError || 'unnamed test') +
(diff ? '\n' + diff : '') +
(source ? '\n| ' + source.trimEnd().split('\n').join('\n| ') : '');
return Object.assign(new Error(name), {
...diag,
stack: `Error: ${name}\n` + expandStack(stack).trimEnd(),
});
};
//# sourceMappingURL=result-to-error.js.map