Skip to content

Commit

Permalink
support 'export default TestEnvironment' (#8163)
Browse files Browse the repository at this point in the history
* support 'export default TestEnvironment'

* Update CHANGELOG.md
  • Loading branch information
jeysal authored and SimenB committed Mar 19, 2019
1 parent d8f43f8 commit 5bb9624
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `[@jest/core]` Filter API pre-filter setup hook ([#8142](https://github.com/facebook/jest/pull/8142))
- `[jest-snapshot]` Improve report when matcher fails, part 14 ([#8132](https://github.com/facebook/jest/pull/8132))
- `[@jest/reporter]` Display todo and skip test descriptions when verbose is true ([#8038](https://github.com/facebook/jest/pull/8038))
- `[jest-runner]` Support default exports for test environments ([#8163](https://github.com/facebook/jest/pull/8163))

### Fixes

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/testEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ it('respects testEnvironment docblock', () => {
const {json: result} = runWithJson('test-environment');

expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(1);
expect(result.numTotalTests).toBe(2);
});
20 changes: 20 additions & 0 deletions e2e/test-environment/EsmDefaultEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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';

exports.__esModule = true;

const NodeEnvironment = require('jest-environment-node');

class Env extends NodeEnvironment {
constructor(...args) {
super(...args);
this.global.property = 'value';
}
}

exports.default = Env;
13 changes: 13 additions & 0 deletions e2e/test-environment/__tests__/esmDefault.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*
* @jest-environment ./EsmDefaultEnvironment.js
*/
'use strict';

test('access env', () => {
expect(property).toBe('value'); // eslint-disable-line no-undef
});
8 changes: 2 additions & 6 deletions packages/jest-core/src/runGlobalHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import {addHook} from 'pirates';
import {Config} from '@jest/types';
import {Test} from 'jest-runner';
import {ScriptTransformer} from '@jest/transform';

// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562
function _interopRequireDefault(obj: any) {
return obj && obj.__esModule ? obj : {default: obj};
}
import {interopRequireDefault} from 'jest-util';

export default async ({
allTests,
Expand Down Expand Up @@ -64,7 +60,7 @@ export default async ({
},
);

const globalModule = _interopRequireDefault(require(modulePath)).default;
const globalModule = interopRequireDefault(require(modulePath)).default;

if (typeof globalModule !== 'function') {
throw new TypeError(
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import {JestEnvironment} from '@jest/environment';
import RuntimeClass from 'jest-runtime';
import fs from 'graceful-fs';
import {ErrorWithStack, setGlobal} from 'jest-util';
import {ErrorWithStack, setGlobal, interopRequireDefault} from 'jest-util';
import LeakDetector from 'jest-leak-detector';
import Resolver from 'jest-resolve';
import {getTestEnvironment} from 'jest-config';
Expand Down Expand Up @@ -104,7 +104,9 @@ async function runTestInternal(
});
}

const TestEnvironment: typeof JestEnvironment = require(testEnvironment);
const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
require(testEnvironment),
).default;
const testFramework: TestFramework =
process.env.JEST_CIRCUS === '1'
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import createDirectory from './createDirectory';
import ErrorWithStack from './ErrorWithStack';
import getFailedSnapshotTests from './getFailedSnapshotTests';
import installCommonGlobals from './installCommonGlobals';
import interopRequireDefault from './interopRequireDefault';
import isInteractive from './isInteractive';
import isPromise from './isPromise';
import setGlobal from './setGlobal';
Expand All @@ -46,6 +47,7 @@ export = {
getConsoleOutput,
getFailedSnapshotTests,
installCommonGlobals,
interopRequireDefault,
isInteractive,
isPromise,
pluralize,
Expand Down
11 changes: 11 additions & 0 deletions packages/jest-util/src/interopRequireDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562
export default function interopRequireDefault(obj: any) {
return obj && obj.__esModule ? obj : {default: obj};
}

0 comments on commit 5bb9624

Please sign in to comment.