From 6c4f951128ef499c6c501645e4d25f56bd00d168 Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Tue, 19 Mar 2019 17:45:28 +0000 Subject: [PATCH 1/2] support 'export default TestEnvironment' --- e2e/__tests__/testEnvironment.test.ts | 2 +- e2e/test-environment/EsmDefaultEnvironment.js | 20 +++++++++++++++++++ .../__tests__/esmDefault.test.js | 13 ++++++++++++ packages/jest-core/src/runGlobalHook.ts | 8 ++------ packages/jest-runner/src/runTest.ts | 6 ++++-- packages/jest-util/src/index.ts | 2 ++ .../jest-util/src/interopRequireDefault.ts | 11 ++++++++++ 7 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 e2e/test-environment/EsmDefaultEnvironment.js create mode 100644 e2e/test-environment/__tests__/esmDefault.test.js create mode 100644 packages/jest-util/src/interopRequireDefault.ts diff --git a/e2e/__tests__/testEnvironment.test.ts b/e2e/__tests__/testEnvironment.test.ts index 605686aa11e6..141fed8eb310 100644 --- a/e2e/__tests__/testEnvironment.test.ts +++ b/e2e/__tests__/testEnvironment.test.ts @@ -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); }); diff --git a/e2e/test-environment/EsmDefaultEnvironment.js b/e2e/test-environment/EsmDefaultEnvironment.js new file mode 100644 index 000000000000..a15261466885 --- /dev/null +++ b/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; diff --git a/e2e/test-environment/__tests__/esmDefault.test.js b/e2e/test-environment/__tests__/esmDefault.test.js new file mode 100644 index 000000000000..a978d0b9e1f6 --- /dev/null +++ b/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 +}); diff --git a/packages/jest-core/src/runGlobalHook.ts b/packages/jest-core/src/runGlobalHook.ts index 32ba09112ec7..7b81a588d521 100644 --- a/packages/jest-core/src/runGlobalHook.ts +++ b/packages/jest-core/src/runGlobalHook.ts @@ -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, @@ -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( diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 7d15b235af71..27eac4a7e75d 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -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'; @@ -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 diff --git a/packages/jest-util/src/index.ts b/packages/jest-util/src/index.ts index 79c797da9d40..6f1f6cf92d19 100644 --- a/packages/jest-util/src/index.ts +++ b/packages/jest-util/src/index.ts @@ -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'; @@ -46,6 +47,7 @@ export = { getConsoleOutput, getFailedSnapshotTests, installCommonGlobals, + interopRequireDefault, isInteractive, isPromise, pluralize, diff --git a/packages/jest-util/src/interopRequireDefault.ts b/packages/jest-util/src/interopRequireDefault.ts new file mode 100644 index 000000000000..471433dcca0f --- /dev/null +++ b/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}; +} From 28822bebf1f4916b58affe5994dda908e206e9bd Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Tue, 19 Mar 2019 17:50:43 +0000 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f3f86083cbe..601c731d8fd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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