Skip to content

Commit 4af727c

Browse files
SimenBcpojer
authored andcommitted
Throw a more useful error when trying to require modules after the test environment is torn down (#5888)
* Throw a more useful error when trying to require modules after the test environment is torn down * remove jest-runtime from stack entirely * fix test in node 6
1 parent 9fdc031 commit 4af727c

File tree

8 files changed

+95
-5
lines changed

8 files changed

+95
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161

6262
### Fixes
6363

64+
* `[jest-runtime]` Throw a more useful error when trying to require modules
65+
after the test environment is torn down
66+
([#5888](https://github.com/facebook/jest/pull/5888))
6467
* `[jest-mock]` [**BREAKING**] Replace timestamps with `invocationCallOrder`
6568
([#5867](https://github.com/facebook/jest/pull/5867))
6669
* `[jest-jasmine2]` Install `sourcemap-support` into normal runtime to catch
@@ -71,8 +74,6 @@
7174
mode. ([#5861](https://github.com/facebook/jest/pull/5861))
7275
* `[jest-mock]` Extend .toHaveBeenCalled return message with outcome
7376
([#5951](https://github.com/facebook/jest/pull/5951))
74-
* `[jest-message-util]` Include column in stack frames
75-
([#5889](https://github.com/facebook/jest/pull/5889))
7677
* `[jest-runner]` Assign `process.env.JEST_WORKER_ID="1"` when in runInBand mode
7778
([#5860](https://github.com/facebook/jest/pull/5860))
7879
* `[jest-cli]` Add descriptive error message when trying to use
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
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.
5+
6+
9 | test('require after done', () => {
7+
10 | setTimeout(() => {
8+
> 11 | const double = require('../');
9+
12 |
10+
13 | expect(double(5)).toBe(10);
11+
14 | }, 0);"
12+
`;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
const runJest = require('../runJest');
13+
14+
test('prints useful error for requires after test is done', () => {
15+
const {stderr} = runJest('require-after-teardown');
16+
17+
const interestingLines = stderr
18+
.split('\n')
19+
.slice(9, 17)
20+
.join('\n');
21+
22+
expect(interestingLines).toMatchSnapshot();
23+
expect(stderr.split('\n')[18]).toMatch(
24+
new RegExp('(__tests__/late-require.test.js:11:20)'),
25+
);
26+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
test('require after done', () => {
10+
setTimeout(() => {
11+
const double = require('../');
12+
13+
expect(double(5)).toBe(10);
14+
}, 0);
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = function double(input) {
9+
return input * 2;
10+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

packages/jest-runtime/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"graceful-fs": "^4.1.11",
1717
"jest-config": "^22.4.2",
1818
"jest-haste-map": "^22.4.2",
19+
"jest-message-util": "^22.4.0",
1920
"jest-regex-util": "^22.1.0",
2021
"jest-resolve": "^22.4.2",
2122
"jest-snapshot": "^22.4.0",

packages/jest-runtime/src/index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {SourceMapRegistry} from 'types/SourceMaps';
1919

2020
import path from 'path';
2121
import HasteMap from 'jest-haste-map';
22+
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
2223
import Resolver from 'jest-resolve';
2324
import {createDirectory, deepCyclicCopy} from 'jest-util';
2425
import {escapePathForRegex} from 'jest-regex-util';
@@ -551,9 +552,28 @@ class Runtime {
551552
}
552553
}
553554

554-
const wrapper = this._environment.runScript(transformedFile.script)[
555-
ScriptTransformer.EVAL_RESULT_VARIABLE
556-
];
555+
const runScript = this._environment.runScript(transformedFile.script);
556+
557+
if (runScript === null) {
558+
const originalStack = new ReferenceError(
559+
'You are trying to `import` a file after the Jest environment has been torn down.',
560+
).stack
561+
.split('\n')
562+
// Remove this file from the stack (jest-message-utils will keep one line)
563+
.filter(line => line.indexOf(__filename) === -1)
564+
.join('\n');
565+
566+
const {message, stack} = separateMessageFromStack(originalStack);
567+
568+
console.error(
569+
`\n${message}\n` +
570+
formatStackTrace(stack, this._config, {noStackTrace: false}),
571+
);
572+
process.exitCode = 1;
573+
return;
574+
}
575+
576+
const wrapper = runScript[ScriptTransformer.EVAL_RESULT_VARIABLE];
557577
wrapper.call(
558578
localModule.exports, // module context
559579
localModule, // module object

0 commit comments

Comments
 (0)