Skip to content

Commit a9e0075

Browse files
authored
fix: remove support for runScript (#11155)
1 parent 46debe9 commit a9e0075

15 files changed

Lines changed: 80 additions & 51 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
3939
- `[jest-core]` Don't report PerformanceObserver as open handle ([#11123](https://github.com/facebook/jest/pull/11123))
4040
- `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766))
41+
- `[jest-environment]` [**BREAKING**] Drop support for `runScript` for test environments ([#11155](https://github.com/facebook/jest/pull/11155))
4142
- `[jest-environment-jsdom]` Use inner realm’s `ArrayBuffer` constructor ([#10885](https://github.com/facebook/jest/pull/10885))
4243
- `[jest-globals]` [**BREAKING**] Disallow return values other than a `Promise` from hooks and tests ([#10512](https://github.com/facebook/jest/pull/10512))
4344
- `[jest-globals]` [**BREAKING**] Disallow mixing a done callback and returning a `Promise` from hooks and tests ([#10512](https://github.com/facebook/jest/pull/10512))

docs/Configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ test('use jsdom in this test file', () => {
10301030
});
10311031
```
10321032

1033-
You can create your own module that will be used for setting up the test environment. The module must export a class with `setup`, `teardown` and `runScript` methods. You can also pass variables from this module to your test suites by assigning them to `this.global` object – this will make them available in your test suites as global variables.
1033+
You can create your own module that will be used for setting up the test environment. The module must export a class with `setup`, `teardown` and `getVmContext` methods. You can also pass variables from this module to your test suites by assigning them to `this.global` object – this will make them available in your test suites as global variables.
10341034

10351035
The class may optionally expose an asynchronous `handleTestEvent` method to bind to events fired by [`jest-circus`](https://github.com/facebook/jest/tree/master/packages/jest-circus). Normally, `jest-circus` test runner would pause until a promise returned from `handleTestEvent` gets fulfilled, **except for the next events**: `start_describe_definition`, `finish_describe_definition`, `add_hook`, `add_test` or `error` (for the up-to-date list you can look at [SyncEvent type in the types definitions](https://github.com/facebook/jest/tree/master/packages/jest-types/src/Circus.ts)). That is caused by backward compatibility reasons and `process.on('unhandledRejection', callback)` signature, but that usually should not be a problem for most of the use cases.
10361036

@@ -1076,8 +1076,8 @@ class CustomEnvironment extends NodeEnvironment {
10761076
await super.teardown();
10771077
}
10781078

1079-
runScript(script) {
1080-
return super.runScript(script);
1079+
getVmContext() {
1080+
return super.getVmContext();
10811081
}
10821082

10831083
async handleTestEvent(event, state) {

docs/Puppeteer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class PuppeteerEnvironment extends NodeEnvironment {
110110
await super.teardown();
111111
}
112112

113-
runScript(script) {
114-
return super.runScript(script);
113+
getVmContext() {
114+
return super.getVmContext();
115115
}
116116
}
117117

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`throw error if test env does not have getVmContext 1`] = `"Test environment found at \\"<rootDir>/EnvUsingRunScript.js\\" does not export a \\"getVmContext\\" method, which is mandatory from Jest 27. This method is a replacement for \\"runScript\\"."`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. 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+
import {resolve, sep} from 'path';
9+
import runJest from '../runJest';
10+
11+
it('throw error if test env does not have getVmContext', () => {
12+
const DIR = resolve(__dirname, '../test-environment-run-script');
13+
const {exitCode, stderr} = runJest(DIR);
14+
15+
expect(stderr.replace(`${DIR}${sep}`, '<rootDir>/')).toMatchSnapshot();
16+
expect(exitCode).toBe(1);
17+
});

e2e/test-environment-async/TestEnvironment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class TestEnvironment extends JSDOMEnvironment {
3434
});
3535
}
3636

37-
runScript(script) {
38-
return super.runScript(script);
37+
getVmContext() {
38+
return super.getVmContext();
3939
}
4040
}
4141

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. 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+
module.exports = class Env {
10+
constructor() {
11+
this.global = global;
12+
this.moduleMocker = {};
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. 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('dummy', () => {
10+
throw new Error('nothing to see here');
11+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "<rootDir>/EnvUsingRunScript.js"
4+
}
5+
}

examples/mongodb/mongo-environment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class MongoEnvironment extends NodeEnvironment {
2929
await super.teardown();
3030
}
3131

32-
runScript(script) {
33-
return super.runScript(script);
32+
getVmContext() {
33+
return super.getVmContext();
3434
}
3535
}
3636

0 commit comments

Comments
 (0)