Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/serialize-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function extractSource(stack, testFile) {

for (const line of stack.split('\n')) {
const callSite = stackUtils.parseLine(line);
if (callSite && normalizeFile(callSite.file) === testFile) {
// Native / eval frames can parse without a file path; skip them.
if (callSite?.file && normalizeFile(callSite.file) === testFile) {
return {
isDependency: false,
isWithinProject: true,
Expand Down
1 change: 1 addition & 0 deletions test-tap/helper/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports.sanitizers = {
experimentalWarning: string => string.replaceAll(/^\(node:\d+\) ExperimentalWarning.+\n/g, ''),
lineEndings: string => string.replaceAll('\r\n', '\n'),
libLineNumbers: string => string.replaceAll(/\((\/lib\/.+\.js):\d+:\d+\)/g, '($1)'),
nodeInternalLineNumbers: string => string.replaceAll(/\((node:internal\/[^)]+):\d+:\d+\)/g, '($1)'),
// The following are injected by tap@18.
posix: string => string.replaceAll('\\', '/'),
tapLoaders: string => string.replaceAll(/.+(Module\._compile|node_modules.pirates|require\.extensions).+\r?\n/g, ''),
Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.edgecases.v22.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ not ok 1 - SyntaxError: Unexpected token 'do'
---
name: SyntaxError
message: Unexpected token 'do'
at: 'compileSourceTextModule (node:internal/modules/esm/utils:346:16)'
at: 'compileSourceTextModule (node:internal/modules/esm/utils)'
...
---tty-stream-chunk-separator
not ok 2 - ast-syntax-error.js exited with a non-zero exit code: 1
Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.edgecases.v24.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ not ok 1 - SyntaxError: Unexpected token 'do'
---
name: SyntaxError
message: Unexpected token 'do'
at: 'compileSourceTextModule (node:internal/modules/esm/utils:318:16)'
at: 'compileSourceTextModule (node:internal/modules/esm/utils)'
...
---tty-stream-chunk-separator
not ok 2 - ast-syntax-error.js exited with a non-zero exit code: 1
Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.edgecases.v26.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ not ok 1 - SyntaxError: Unexpected token 'do'
---
name: SyntaxError
message: Unexpected token 'do'
at: 'compileSourceTextModule (node:internal/modules/esm/utils:354:16)'
at: 'compileSourceTextModule (node:internal/modules/esm/utils)'
...
---tty-stream-chunk-separator
not ok 2 - ast-syntax-error.js exited with a non-zero exit code: 1
Expand Down
13 changes: 13 additions & 0 deletions test-tap/reporters/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test(async t => {
report.sanitizers.esmLoader,
report.sanitizers.experimentalWarning,
report.sanitizers.libLineNumbers,
report.sanitizers.nodeInternalLineNumbers,
report.sanitizers.posix,
report.sanitizers.tapLoaders,
report.sanitizers.timers,
Expand All @@ -50,4 +51,16 @@ test(async t => {
t.test('tap reporter - second failFast run', run('failFast2'));
t.test('tap reporter - only run', run('only'));
t.test('tap reporter - edge cases', run('edgeCases'));
t.test('Node.js internal stack locations are stable across runtime releases', t => {
for (const location of ['318:16', '319:16', '346:16', '355:16']) {
t.equal(
report.sanitizers.nodeInternalLineNumbers(`compileSourceTextModule (node:internal/modules/esm/utils:${location})`),
'compileSourceTextModule (node:internal/modules/esm/utils)',
);
}

const applicationFrame = 'test (file:///project/test.js:3:1)';
t.equal(report.sanitizers.nodeInternalLineNumbers(applicationFrame), applicationFrame);
t.end();
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cool with not having the line numbers in the logs (unrelated though it is to what this PR fixes), but this test adds no value.

Suggested change
t.test('Node.js internal stack locations are stable across runtime releases', t => {
for (const location of ['318:16', '319:16', '346:16', '355:16']) {
t.equal(
report.sanitizers.nodeInternalLineNumbers(`compileSourceTextModule (node:internal/modules/esm/utils:${location})`),
'compileSourceTextModule (node:internal/modules/esm/utils)',
);
}
const applicationFrame = 'test (file:///project/test.js:3:1)';
t.equal(report.sanitizers.nodeInternalLineNumbers(applicationFrame), applicationFrame);
t.end();
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the reporter sanitizer and the edge-case log refreshes in 7216266 — they were unrelated to the serialize-error fix. The PR is now only lib/serialize-error.js + test/serialize-error.js.

});
46 changes: 46 additions & 0 deletions test-tap/serialize-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import path from 'node:path';
import {pathToFileURL} from 'node:url';

import {test} from 'tap';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests should be under test/.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved in the earlier follow-up — coverage now lives in test/serialize-error.js (and the old test-tap/serialize-error.js path is gone).


import serializeError from '../lib/serialize-error.js';

test('extractSource skips stack frames without a file path', t => {
const testFilePath = path.resolve('test-fixture.js');
const testFile = pathToFileURL(testFilePath).toString();
const error = new Error('boom');
// Stack-utils leaves `file` undefined for native frames; previously
// normalizeFile(undefined) threw and hid the original test failure.
error.stack = `Error: boom
at Function.foo (native)
at Object.<anonymous> (${testFilePath}:10:5)`;

const result = serializeError(error, {testFile});
t.equal(result.type, 'native');
t.same(result.source, {
isDependency: false,
isWithinProject: true,
file: testFile,
line: 10,
});
t.end();
});

test('extractSource skips anonymous file paths that are not the test file', t => {
const testFilePath = path.resolve('test-fixture.js');
const testFile = pathToFileURL(testFilePath).toString();
const error = new Error('boom');
error.stack = `Error: boom
at Object.<anonymous> (<anonymous>:1:2)
at Object.<anonymous> (${testFilePath}:4:1)`;

const result = serializeError(error, {testFile});
t.equal(result.type, 'native');
t.same(result.source, {
isDependency: false,
isWithinProject: true,
file: testFile,
line: 4,
});
t.end();
});
Loading