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

support 'export default TestEnvironment' #8163

Merged
merged 2 commits into from Mar 19, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[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
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
@@ -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
@@ -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
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
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
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
@@ -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};
}