Skip to content

Commit 5bb9624

Browse files
jeysalSimenB
authored andcommitted
support 'export default TestEnvironment' (#8163)
* support 'export default TestEnvironment' * Update CHANGELOG.md
1 parent d8f43f8 commit 5bb9624

8 files changed

Lines changed: 54 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `[@jest/core]` Filter API pre-filter setup hook ([#8142](https://github.com/facebook/jest/pull/8142))
66
- `[jest-snapshot]` Improve report when matcher fails, part 14 ([#8132](https://github.com/facebook/jest/pull/8132))
77
- `[@jest/reporter]` Display todo and skip test descriptions when verbose is true ([#8038](https://github.com/facebook/jest/pull/8038))
8+
- `[jest-runner]` Support default exports for test environments ([#8163](https://github.com/facebook/jest/pull/8163))
89

910
### Fixes
1011

e2e/__tests__/testEnvironment.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ it('respects testEnvironment docblock', () => {
1515
const {json: result} = runWithJson('test-environment');
1616

1717
expect(result.success).toBe(true);
18-
expect(result.numTotalTests).toBe(1);
18+
expect(result.numTotalTests).toBe(2);
1919
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
exports.__esModule = true;
10+
11+
const NodeEnvironment = require('jest-environment-node');
12+
13+
class Env extends NodeEnvironment {
14+
constructor(...args) {
15+
super(...args);
16+
this.global.property = 'value';
17+
}
18+
}
19+
20+
exports.default = Env;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
* @jest-environment ./EsmDefaultEnvironment.js
8+
*/
9+
'use strict';
10+
11+
test('access env', () => {
12+
expect(property).toBe('value'); // eslint-disable-line no-undef
13+
});

packages/jest-core/src/runGlobalHook.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import {addHook} from 'pirates';
1111
import {Config} from '@jest/types';
1212
import {Test} from 'jest-runner';
1313
import {ScriptTransformer} from '@jest/transform';
14-
15-
// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562
16-
function _interopRequireDefault(obj: any) {
17-
return obj && obj.__esModule ? obj : {default: obj};
18-
}
14+
import {interopRequireDefault} from 'jest-util';
1915

2016
export default async ({
2117
allTests,
@@ -64,7 +60,7 @@ export default async ({
6460
},
6561
);
6662

67-
const globalModule = _interopRequireDefault(require(modulePath)).default;
63+
const globalModule = interopRequireDefault(require(modulePath)).default;
6864

6965
if (typeof globalModule !== 'function') {
7066
throw new TypeError(

packages/jest-runner/src/runTest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import {JestEnvironment} from '@jest/environment';
2020
import RuntimeClass from 'jest-runtime';
2121
import fs from 'graceful-fs';
22-
import {ErrorWithStack, setGlobal} from 'jest-util';
22+
import {ErrorWithStack, setGlobal, interopRequireDefault} from 'jest-util';
2323
import LeakDetector from 'jest-leak-detector';
2424
import Resolver from 'jest-resolve';
2525
import {getTestEnvironment} from 'jest-config';
@@ -104,7 +104,9 @@ async function runTestInternal(
104104
});
105105
}
106106

107-
const TestEnvironment: typeof JestEnvironment = require(testEnvironment);
107+
const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
108+
require(testEnvironment),
109+
).default;
108110
const testFramework: TestFramework =
109111
process.env.JEST_CIRCUS === '1'
110112
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies

packages/jest-util/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import createDirectory from './createDirectory';
2020
import ErrorWithStack from './ErrorWithStack';
2121
import getFailedSnapshotTests from './getFailedSnapshotTests';
2222
import installCommonGlobals from './installCommonGlobals';
23+
import interopRequireDefault from './interopRequireDefault';
2324
import isInteractive from './isInteractive';
2425
import isPromise from './isPromise';
2526
import setGlobal from './setGlobal';
@@ -46,6 +47,7 @@ export = {
4647
getConsoleOutput,
4748
getFailedSnapshotTests,
4849
installCommonGlobals,
50+
interopRequireDefault,
4951
isInteractive,
5052
isPromise,
5153
pluralize,
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+
8+
// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562
9+
export default function interopRequireDefault(obj: any) {
10+
return obj && obj.__esModule ? obj : {default: obj};
11+
}

0 commit comments

Comments
 (0)