Skip to content

Commit 31cad20

Browse files
authored
fix: include path to test file in "after teardown" error (#11885)
1 parent 7c4ea24 commit 31cad20

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### Fixes
66

77
- `[jest-runtime]` Fix regression when using `jest.isolateModules` and mocks ([#11882](https://github.com/facebook/jest/pull/11882))
8+
- `[jest-runtime]` Include test name when importing modules after test has completed ([#11885](https://github.com/facebook/jest/pull/11885))
9+
- `[jest-runtime]` Error when ESM import is used after test is torn down ([#11885](https://github.com/facebook/jest/pull/11885))
810

911
### Chore & Maintenance
1012

e2e/__tests__/__snapshots__/environmentAfterTeardown.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`prints useful error for environment methods after test is done 1`] = `
4-
ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.
4+
ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js.
55
66
9 | test('access environment methods after done', () => {
77
10 | setTimeout(() => {

e2e/__tests__/__snapshots__/requireAfterTeardown.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`prints useful error for requires after test is done 1`] = `
4-
ReferenceError: You are trying to \`import\` a file after the Jest environment has been torn down.
4+
ReferenceError: You are trying to \`import\` a file after the Jest environment has been torn down. From __tests__/lateRequire.test.js.
55
66
9 | test('require after done', () => {
77
10 | setTimeout(() => {

e2e/__tests__/environmentAfterTeardown.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ test('prints useful error for environment methods after test is done', () => {
1414

1515
expect(wrap(interestingLines)).toMatchSnapshot();
1616
expect(stderr.split('\n')[9]).toBe(
17-
'ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.',
17+
'ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down. From __tests__/afterTeardown.test.js.',
1818
);
1919
});

packages/jest-runner/src/runTest.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,8 @@ async function runTestInternal(
329329
setImmediate(() => resolve({leakDetector, result}));
330330
});
331331
} finally {
332+
runtime.teardown();
332333
await environment.teardown();
333-
// TODO: this function might be missing, remove ? in Jest 26
334-
runtime.teardown?.();
335334

336335
sourcemapSupport.resetRetrieveHandlers();
337336
}

packages/jest-runtime/src/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {parse as parseCjs} from 'cjs-module-lexer';
2222
import {CoverageInstrumenter, V8Coverage} from 'collect-v8-coverage';
2323
import execa = require('execa');
2424
import * as fs from 'graceful-fs';
25+
import slash = require('slash');
2526
import stripBOM = require('strip-bom');
2627
import type {
2728
Jest,
@@ -226,6 +227,7 @@ export default class Runtime {
226227
private jestGlobals?: JestGlobals;
227228
private readonly esmConditions: Array<string>;
228229
private readonly cjsConditions: Array<string>;
230+
private isTornDown = false;
229231

230232
constructor(
231233
config: Config.ProjectConfig,
@@ -531,6 +533,14 @@ export default class Runtime {
531533
referencingIdentifier: string,
532534
context: VMContext,
533535
) {
536+
if (this.isTornDown) {
537+
this._logFormattedReferenceError(
538+
'You are trying to `import` a file after the Jest environment has been torn down.',
539+
);
540+
process.exitCode = 1;
541+
return;
542+
}
543+
534544
if (specifier === '@jest/globals') {
535545
const fromCache = this._esmoduleRegistry.get('@jest/globals');
536546

@@ -575,6 +585,14 @@ export default class Runtime {
575585
}
576586

577587
private async linkAndEvaluateModule(module: VMModule) {
588+
if (this.isTornDown) {
589+
this._logFormattedReferenceError(
590+
'You are trying to `import` a file after the Jest environment has been torn down.',
591+
);
592+
process.exitCode = 1;
593+
return;
594+
}
595+
578596
if (module.status === 'unlinked') {
579597
// since we might attempt to link the same module in parallel, stick the promise in a weak map so every call to
580598
// this method can await it
@@ -1199,6 +1217,8 @@ export default class Runtime {
11991217
this._v8CoverageResult = [];
12001218
this._v8CoverageInstrumenter = undefined;
12011219
this._moduleImplementation = undefined;
1220+
1221+
this.isTornDown = true;
12021222
}
12031223

12041224
private _resolveModule(
@@ -1284,6 +1304,14 @@ export default class Runtime {
12841304
moduleRegistry: ModuleRegistry,
12851305
from: Config.Path | null,
12861306
) {
1307+
if (this.isTornDown) {
1308+
this._logFormattedReferenceError(
1309+
'You are trying to `import` a file after the Jest environment has been torn down.',
1310+
);
1311+
process.exitCode = 1;
1312+
return;
1313+
}
1314+
12871315
// If the environment was disposed, prevent this module from being executed.
12881316
if (!this._environment.global) {
12891317
return;
@@ -1847,6 +1875,7 @@ export default class Runtime {
18471875
};
18481876
const _getFakeTimers = () => {
18491877
if (
1878+
this.isTornDown ||
18501879
!(this._environment.fakeTimers || this._environment.fakeTimersModern)
18511880
) {
18521881
this._logFormattedReferenceError(
@@ -1975,7 +2004,10 @@ export default class Runtime {
19752004
}
19762005

19772006
private _logFormattedReferenceError(errorMessage: string) {
1978-
const originalStack = new ReferenceError(errorMessage)
2007+
const testPath = this._testPath
2008+
? ` From ${slash(path.relative(this._config.rootDir, this._testPath))}.`
2009+
: '';
2010+
const originalStack = new ReferenceError(`${errorMessage}${testPath}`)
19792011
.stack!.split('\n')
19802012
// Remove this file from the stack (jest-message-utils will keep one line)
19812013
.filter(line => line.indexOf(__filename) === -1)

0 commit comments

Comments
 (0)