Skip to content

Commit

Permalink
fix: include path to test file in "after teardown" error (#11885)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 20, 2021
1 parent 7c4ea24 commit 31cad20
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Fixes

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

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints useful error for environment methods after test is done 1`] = `
ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.
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.
9 | test('access environment methods after done', () => {
10 | setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints useful error for requires after test is done 1`] = `
ReferenceError: You are trying to \`import\` a file after the Jest environment has been torn down.
ReferenceError: You are trying to \`import\` a file after the Jest environment has been torn down. From __tests__/lateRequire.test.js.
9 | test('require after done', () => {
10 | setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/environmentAfterTeardown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ test('prints useful error for environment methods after test is done', () => {

expect(wrap(interestingLines)).toMatchSnapshot();
expect(stderr.split('\n')[9]).toBe(
'ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.',
'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.',
);
});
3 changes: 1 addition & 2 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,8 @@ async function runTestInternal(
setImmediate(() => resolve({leakDetector, result}));
});
} finally {
runtime.teardown();
await environment.teardown();
// TODO: this function might be missing, remove ? in Jest 26
runtime.teardown?.();

sourcemapSupport.resetRetrieveHandlers();
}
Expand Down
34 changes: 33 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {parse as parseCjs} from 'cjs-module-lexer';
import {CoverageInstrumenter, V8Coverage} from 'collect-v8-coverage';
import execa = require('execa');
import * as fs from 'graceful-fs';
import slash = require('slash');
import stripBOM = require('strip-bom');
import type {
Jest,
Expand Down Expand Up @@ -226,6 +227,7 @@ export default class Runtime {
private jestGlobals?: JestGlobals;
private readonly esmConditions: Array<string>;
private readonly cjsConditions: Array<string>;
private isTornDown = false;

constructor(
config: Config.ProjectConfig,
Expand Down Expand Up @@ -531,6 +533,14 @@ export default class Runtime {
referencingIdentifier: string,
context: VMContext,
) {
if (this.isTornDown) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
process.exitCode = 1;
return;
}

if (specifier === '@jest/globals') {
const fromCache = this._esmoduleRegistry.get('@jest/globals');

Expand Down Expand Up @@ -575,6 +585,14 @@ export default class Runtime {
}

private async linkAndEvaluateModule(module: VMModule) {
if (this.isTornDown) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
process.exitCode = 1;
return;
}

if (module.status === 'unlinked') {
// since we might attempt to link the same module in parallel, stick the promise in a weak map so every call to
// this method can await it
Expand Down Expand Up @@ -1199,6 +1217,8 @@ export default class Runtime {
this._v8CoverageResult = [];
this._v8CoverageInstrumenter = undefined;
this._moduleImplementation = undefined;

this.isTornDown = true;
}

private _resolveModule(
Expand Down Expand Up @@ -1284,6 +1304,14 @@ export default class Runtime {
moduleRegistry: ModuleRegistry,
from: Config.Path | null,
) {
if (this.isTornDown) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
process.exitCode = 1;
return;
}

// If the environment was disposed, prevent this module from being executed.
if (!this._environment.global) {
return;
Expand Down Expand Up @@ -1847,6 +1875,7 @@ export default class Runtime {
};
const _getFakeTimers = () => {
if (
this.isTornDown ||
!(this._environment.fakeTimers || this._environment.fakeTimersModern)
) {
this._logFormattedReferenceError(
Expand Down Expand Up @@ -1975,7 +2004,10 @@ export default class Runtime {
}

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

0 comments on commit 31cad20

Please sign in to comment.