Skip to content

Commit 7ed2320

Browse files
committed
Shorter output for reference errors
Because imports are processed asynchronously, an error within one import causes the test runtime to abort and be torn down, which causes the other imports to log numerous redundant errors. Because these errors have no stack traces, they're even more verbose than necessary, with lots of blank lines. Sample output for one of my projects: ``` (node:53121) ExperimentalWarning: VM Modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) FAIL lib src/plots/__tests__/timelineChart.test.ts ● Test suite failed to run Cannot find module './chartJs/controller.polar.js' from 'src/plots/chartJsInit.ts' at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/index.js:863:11) ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.425 s Ran all test suites matching src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. ``` To address this, I added logic to suppress redundant errors and to only include stack trace newlines if a stack trace is present. Sample output after this change: ``` FAIL lib src/plots/__tests__/timelineChart.test.ts ● Test suite failed to run Cannot find module './chartJs/controller.polar.js' from 'src/plots/chartJsInit.ts' at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/index.js:864:11) ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/plots/__tests__/timelineChart.test.ts. Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.514 s ```
1 parent fe7f28c commit 7ed2320

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## main
22

3+
### Features
4+
5+
- `[jest-runtime]` Reduce redundant ReferenceError messages
6+
37
## 30.0.5
48

59
### Features

packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ exports[`formatStackTrace should properly handle string causes 1`] = `
7575
7676
Cause:
7777
string cause
78-
<dim></intensity>
79-
<dim></intensity>
78+
8079
"
8180
`;
8281

packages/jest-message-util/src/index.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ const removeInternalStackEntries = (
242242
let pathCounter = 0;
243243

244244
return lines.filter(line => {
245+
if (!line) {
246+
return false;
247+
}
248+
245249
if (ANONYMOUS_FN_IGNORE.test(line)) {
246250
return false;
247251
}
@@ -368,17 +372,18 @@ export function formatStackTrace(
368372
}
369373
}
370374

371-
const stacktrace = lines
372-
.filter(Boolean)
373-
.map(
374-
line =>
375-
STACK_INDENT + formatPath(trimPaths(line), config, relativeTestPath),
376-
)
377-
.join('\n');
378-
379-
return renderedCallsite
380-
? `${renderedCallsite}\n${stacktrace}`
381-
: `\n${stacktrace}`;
375+
const stacktrace =
376+
lines.length === 0
377+
? ''
378+
: `\n${lines
379+
.map(
380+
line =>
381+
STACK_INDENT +
382+
formatPath(trimPaths(line), config, relativeTestPath),
383+
)
384+
.join('\n')}`;
385+
386+
return renderedCallsite + stacktrace;
382387
}
383388

384389
type FailedResults = Array<{

packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
exports[`annotations logs error annotation when a test has reference error 1`] = `
44
Array [
55
"
6-
::error file=/user/project/__tests__/example.test.js,line=25,title=example test::ReferenceError: abc is not defined%0A%0A at Object.abc (__tests__/example.test.js:25:12)
6+
::error file=/user/project/__tests__/example.test.js,line=25,title=example test::ReferenceError: abc is not defined%0A at Object.abc (__tests__/example.test.js:25:12)
77
",
88
]
99
`;
1010

1111
exports[`annotations logs error annotation when an expectation fails to pass 1`] = `
1212
Array [
1313
"
14-
::error file=/user/project/__tests__/example.test.js,line=20,title=example test::expect(received).toBe(expected) // Object.is equality%0A%0AExpected: 1%0AReceived: 10%0A%0A at Object.toBe (__tests__/example.test.js:20:14)
14+
::error file=/user/project/__tests__/example.test.js,line=20,title=example test::expect(received).toBe(expected) // Object.is equality%0A%0AExpected: 1%0AReceived: 10%0A at Object.toBe (__tests__/example.test.js:20:14)
1515
",
1616
]
1717
`;
1818

1919
exports[`annotations logs error annotation when test is wrapped in describe block 1`] = `
2020
Array [
2121
"
22-
::error file=/user/project/__tests__/example.test.js,line=20,title=describe › example test::expect(received).toBe(expected) // Object.is equality%0A%0AExpected: 1%0AReceived: 10%0A%0A at Object.toBe (__tests__/example.test.js:20:14)
22+
::error file=/user/project/__tests__/example.test.js,line=20,title=describe › example test::expect(received).toBe(expected) // Object.is equality%0A%0AExpected: 1%0AReceived: 10%0A at Object.toBe (__tests__/example.test.js:20:14)
2323
",
2424
]
2525
`;
@@ -28,12 +28,12 @@ exports[`annotations logs warning annotation before logging errors when test res
2828
Array [
2929
Array [
3030
"
31-
::warning file=/user/project/__tests__/example.test.js,line=19,title=RETRY 1: example test::expect(received).toBeFalsy()%0A%0AReceived: true%0A%0A at Object.toBeFalsy (__tests__/example.test.js:19:20)
31+
::warning file=/user/project/__tests__/example.test.js,line=19,title=RETRY 1: example test::expect(received).toBeFalsy()%0A%0AReceived: true%0A at Object.toBeFalsy (__tests__/example.test.js:19:20)
3232
",
3333
],
3434
Array [
3535
"
36-
::error file=/user/project/__tests__/example.test.js,line=19,title=example test::expect(received).toBeFalsy()%0A%0AReceived: true%0A%0A at Object.toBeFalsy (__tests__/example.test.js:19:20)
36+
::error file=/user/project/__tests__/example.test.js,line=19,title=example test::expect(received).toBeFalsy()%0A%0AReceived: true%0A at Object.toBeFalsy (__tests__/example.test.js:19:20)
3737
",
3838
],
3939
]

packages/jest-runtime/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export default class Runtime {
213213
private readonly cjsConditions: Array<string>;
214214
private isTornDown = false;
215215
private isInsideTestCode: boolean | undefined;
216+
private readonly loggedReferenceErrors = new Set<string>();
216217

217218
constructor(
218219
config: Config.ProjectConfig,
@@ -2461,11 +2462,14 @@ export default class Runtime {
24612462

24622463
const {message, stack} = separateMessageFromStack(originalStack);
24632464

2464-
console.error(
2465-
`\n${message}\n${formatStackTrace(stack, this._config, {
2466-
noStackTrace: false,
2467-
})}`,
2468-
);
2465+
const stackTrace = formatStackTrace(stack, this._config, {
2466+
noStackTrace: false,
2467+
});
2468+
const formattedMessage = `\n${message}${stackTrace ? `\n${stackTrace}` : ''}`;
2469+
if (!this.loggedReferenceErrors.has(formattedMessage)) {
2470+
console.error(formattedMessage);
2471+
this.loggedReferenceErrors.add(formattedMessage);
2472+
}
24692473
}
24702474

24712475
private constructInjectedModuleParameters(): Array<string> {

0 commit comments

Comments
 (0)