Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw a more useful error when trying to require modules after the test environment is torn down #5888

Merged
merged 4 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@

### Fixes

* `[jest-runtime]` Throw a more useful error when trying to require modules
after the test environment is torn down
([#5888](https://github.com/facebook/jest/pull/5888))
* `[jest-mock]` [**BREAKING**] Replace timestamps with `invocationCallOrder`
([#5867](https://github.com/facebook/jest/pull/5867))
* `[jest-jasmine2]` Install `sourcemap-support` into normal runtime to catch
Expand All @@ -71,8 +74,6 @@
mode. ([#5861](https://github.com/facebook/jest/pull/5861))
* `[jest-mock]` Extend .toHaveBeenCalled return message with outcome
([#5951](https://github.com/facebook/jest/pull/5951))
* `[jest-message-util]` Include column in stack frames
([#5889](https://github.com/facebook/jest/pull/5889))
* `[jest-runner]` Assign `process.env.JEST_WORKER_ID="1"` when in runInBand mode
([#5860](https://github.com/facebook/jest/pull/5860))
* `[jest-cli]` Add descriptive error message when trying to use
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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.

9 | test('require after done', () => {
10 | setTimeout(() => {
> 11 | const double = require('../');
12 |
13 | expect(double(5)).toBe(10);
14 | }, 0);"
`;
26 changes: 26 additions & 0 deletions integration-tests/__tests__/require_after_teardown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

const runJest = require('../runJest');

test('prints useful error for requires after test is done', () => {
const {stderr} = runJest('require-after-teardown');

const interestingLines = stderr
.split('\n')
.slice(9, 17)
.join('\n');

expect(interestingLines).toMatchSnapshot();
expect(stderr.split('\n')[18]).toMatch(
new RegExp('(__tests__/late-require.test.js:11:20)'),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('require after done', () => {
setTimeout(() => {
const double = require('../');

expect(double(5)).toBe(10);
}, 0);
});
10 changes: 10 additions & 0 deletions integration-tests/require-after-teardown/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = function double(input) {
return input * 2;
};
5 changes: 5 additions & 0 deletions integration-tests/require-after-teardown/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
1 change: 1 addition & 0 deletions packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"graceful-fs": "^4.1.11",
"jest-config": "^22.4.2",
"jest-haste-map": "^22.4.2",
"jest-message-util": "^22.4.0",
"jest-regex-util": "^22.1.0",
"jest-resolve": "^22.4.2",
"jest-snapshot": "^22.4.0",
Expand Down
26 changes: 23 additions & 3 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {SourceMapRegistry} from 'types/SourceMaps';

import path from 'path';
import HasteMap from 'jest-haste-map';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import Resolver from 'jest-resolve';
import {createDirectory, deepCyclicCopy} from 'jest-util';
import {escapePathForRegex} from 'jest-regex-util';
Expand Down Expand Up @@ -551,9 +552,28 @@ class Runtime {
}
}

const wrapper = this._environment.runScript(transformedFile.script)[
ScriptTransformer.EVAL_RESULT_VARIABLE
];
const runScript = this._environment.runScript(transformedFile.script);

if (runScript === null) {
const originalStack = new ReferenceError(
Copy link
Member Author

Choose a reason for hiding this comment

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

this is added in a second commit so easy to back out, not sure if it's needed. Without it you get this:
image

So not too bad either way 🙂

'You are trying to `import` a file after the Jest environment has been torn down.',
).stack
.split('\n')
// Remove this file from the stack (jest-message-utils will keep one line)
.filter(line => line.indexOf(__filename) === -1)
.join('\n');

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

console.error(
`\n${message}\n` +
formatStackTrace(stack, this._config, {noStackTrace: false}),
);
process.exitCode = 1;
return;
}

const wrapper = runScript[ScriptTransformer.EVAL_RESULT_VARIABLE];
wrapper.call(
localModule.exports, // module context
localModule, // module object
Expand Down