diff --git a/CHANGELOG.md b/CHANGELOG.md index 4215de6b474b..2ad55d074c54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,6 @@ - `[expect]` Support `async function`s in `toThrow` ([#9817](https://github.com/facebook/jest/pull/9817)) - `[jest-console]` Add code frame to `console.error` and `console.warn` ([#9741](https://github.com/facebook/jest/pull/9741)) -- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801)) - `[jest-runtime, jest-jasmine2, jest-circus]` Experimental, limited ECMAScript Modules support ([#9772](https://github.com/facebook/jest/pull/9772)) ### Fixes diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index bbbc7d526b05..d01f975884b8 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -3,7 +3,7 @@ id: api title: Globals --- -In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do `import {describe, expect, it} from '@jest/globals'`. +In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. ## Methods diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 784c5c3370f1..638e16f38550 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -3,7 +3,7 @@ id: jest-object title: The Jest Object --- -The `jest` object is automatically in scope within every test file. The methods in the `jest` object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via `import {jest} from '@jest/globals'`. +The `jest` object is automatically in scope within every test file. The methods in the `jest` object help create mocks and let you control Jest's overall behavior. ## Mock Modules diff --git a/e2e/__tests__/importedGlobals.test.ts b/e2e/__tests__/importedGlobals.test.ts deleted file mode 100644 index 146ed6544715..000000000000 --- a/e2e/__tests__/importedGlobals.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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. - */ - -import runJest from '../runJest'; - -test('imported globals', () => { - const result = runJest('imported-globals'); - expect(result.exitCode).toBe(0); -}); diff --git a/e2e/imported-globals/__tests__/env.test.js b/e2e/imported-globals/__tests__/env.test.js deleted file mode 100644 index e81a1db5a6d5..000000000000 --- a/e2e/imported-globals/__tests__/env.test.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * 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. - */ - -import { - expect as importedExpect, - jest as importedJest, - test as importedTest, -} from '@jest/globals'; - -importedTest('they match the globals', () => { - importedExpect(importedExpect).toBe(expect); - importedExpect(importedJest).toBe(jest); - importedExpect(importedTest).toBe(test); - - expect(importedExpect).toBe(expect); - expect(importedJest).toBe(jest); - expect(importedTest).toBe(test); -}); diff --git a/e2e/imported-globals/babel.config.js b/e2e/imported-globals/babel.config.js deleted file mode 100644 index 9be036106918..000000000000 --- a/e2e/imported-globals/babel.config.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * 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. - */ - -module.exports = require('../../babel.config'); diff --git a/e2e/imported-globals/package.json b/e2e/imported-globals/package.json deleted file mode 100644 index 148788b25446..000000000000 --- a/e2e/imported-globals/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "jest": { - "testEnvironment": "node" - } -} diff --git a/packages/jest-globals/.npmignore b/packages/jest-globals/.npmignore deleted file mode 100644 index 3ee5d55b0b89..000000000000 --- a/packages/jest-globals/.npmignore +++ /dev/null @@ -1,5 +0,0 @@ -**/__mocks__/** -**/__tests__/** -src -tsconfig.json -tsconfig.tsbuildinfo diff --git a/packages/jest-globals/package.json b/packages/jest-globals/package.json deleted file mode 100644 index 80ac807c888e..000000000000 --- a/packages/jest-globals/package.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "@jest/globals", - "version": "25.3.0", - "repository": { - "type": "git", - "url": "https://github.com/facebook/jest.git", - "directory": "packages/jest-globals" - }, - "engines": { - "node": ">= 8.3" - }, - "license": "MIT", - "main": "build/index.js", - "types": "build/index.d.ts", - "typesVersions": { - "<3.8": { - "build/*": [ - "build/ts3.4/*" - ] - } - }, - "dependencies": { - "@jest/environment": "^25.3.0", - "@jest/types": "^25.3.0", - "expect": "^25.3.0" - }, - "publishConfig": { - "access": "public" - } -} diff --git a/packages/jest-globals/src/__tests__/index.ts b/packages/jest-globals/src/__tests__/index.ts deleted file mode 100644 index cb0af7398581..000000000000 --- a/packages/jest-globals/src/__tests__/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -/** - * 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. - */ - -test('throw when directly imported', () => { - expect(() => require('../')).toThrowError( - 'Do not import `@jest/globals` outside of the Jest test environment', - ); -}); diff --git a/packages/jest-globals/src/index.ts b/packages/jest-globals/src/index.ts deleted file mode 100644 index 82bc15a3b522..000000000000 --- a/packages/jest-globals/src/index.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * 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. - */ - -import importedExpect = require('expect'); -import type {Jest} from '@jest/environment'; -import type {Global} from '@jest/types'; - -export declare type jest = Jest; - -export declare type expect = typeof importedExpect; - -export declare type it = Global.GlobalAdditions['it']; -export declare type test = Global.GlobalAdditions['test']; -export declare type fit = Global.GlobalAdditions['fit']; -export declare type xit = Global.GlobalAdditions['xit']; -export declare type xtest = Global.GlobalAdditions['xtest']; -export declare type describe = Global.GlobalAdditions['describe']; -export declare type xdescribe = Global.GlobalAdditions['xdescribe']; -export declare type fdescribe = Global.GlobalAdditions['fdescribe']; -export declare type beforeAll = Global.GlobalAdditions['beforeAll']; -export declare type beforeEach = Global.GlobalAdditions['beforeEach']; -export declare type afterEach = Global.GlobalAdditions['afterEach']; -export declare type afterAll = Global.GlobalAdditions['afterAll']; - -throw new Error( - 'Do not import `@jest/globals` outside of the Jest test environment', -); diff --git a/packages/jest-globals/tsconfig.json b/packages/jest-globals/tsconfig.json deleted file mode 100644 index b6678e5f0aed..000000000000 --- a/packages/jest-globals/tsconfig.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "extends": "../../tsconfig.json", - "compilerOptions": { - // we don't want `@types/jest` to be referenced - "types": ["node"], - "rootDir": "src", - "outDir": "build" - }, - "references": [ - {"path": "../expect"}, - {"path": "../jest-environment"}, - {"path": "../jest-types"} - ] -} diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index 822778a4f389..bf389a1ef25f 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -19,7 +19,6 @@ "dependencies": { "@jest/console": "^25.3.0", "@jest/environment": "^25.3.0", - "@jest/globals": "^25.3.0", "@jest/source-map": "^25.2.6", "@jest/test-result": "^25.3.0", "@jest/transform": "^25.3.0", diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 1a5cff8361a7..5e0c5ff8ec70 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -19,7 +19,7 @@ import { compileFunction, } from 'vm'; import * as nativeModule from 'module'; -import type {Config, Global} from '@jest/types'; +import type {Config} from '@jest/types'; import type { Jest, JestEnvironment, @@ -27,7 +27,6 @@ import type { Module, ModuleWrapper, } from '@jest/environment'; -import type * as JestGlobals from '@jest/globals'; import type {SourceMapRegistry} from '@jest/source-map'; import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; import {createDirectory, deepCyclicCopy} from 'jest-util'; @@ -53,11 +52,6 @@ import Resolver = require('jest-resolve'); import Snapshot = require('jest-snapshot'); import stripBOM = require('strip-bom'); -interface JestGlobalsValues extends Global.TestFrameworkGlobals { - jest: JestGlobals.jest; - expect: JestGlobals.expect; -} - type HasteMapOptions = { console?: Console; maxWorkers: number; @@ -152,7 +146,6 @@ class Runtime { private _unmockList: RegExp | undefined; private _virtualMocks: BooleanObject; private _moduleImplementation?: typeof nativeModule.Module; - private jestObjectCaches: Map; constructor( config: Config.ProjectConfig, @@ -190,7 +183,6 @@ class Runtime { this._sourceMapRegistry = Object.create(null); this._fileTransforms = new Map(); this._virtualMocks = Object.create(null); - this.jestObjectCaches = new Map(); this._mockMetaDataCache = Object.create(null); this._shouldMockModuleCache = Object.create(null); @@ -428,11 +420,6 @@ class Runtime { modulePath = manualMock; } - if (moduleName === '@jest/globals') { - // @ts-ignore: we don't care that it's not assignable to T - return this.getGlobalsForFile(from); - } - if (moduleName && this._resolver.isCoreModule(moduleName)) { return this._requireCoreModule(moduleName); } @@ -955,13 +942,6 @@ class Runtime { return; } - const jestObject = this._createJestObjectFor( - filename, - localModule.require as LocalModuleRequire, - ); - - this.jestObjectCaches.set(filename, jestObject); - try { compiledFunction.call( localModule.exports, @@ -971,7 +951,10 @@ class Runtime { dirname, // __dirname filename, // __filename this._environment.global, // global object - jestObject, // jest object + this._createJestObjectFor( + filename, + localModule.require as LocalModuleRequire, + ), // jest object ...this._config.extraGlobals.map(globalVariable => { if (this._environment.global[globalVariable]) { return this._environment.global[globalVariable]; @@ -1483,30 +1466,6 @@ class Runtime { throw e; } - - private getGlobalsForFile(from: Config.Path): JestGlobalsValues { - const jest = this.jestObjectCaches.get(from); - - // This won't exist in ESM - invariant(jest, 'There should always be a Jest object already'); - - return { - afterAll: this._environment.global.afterAll, - afterEach: this._environment.global.afterEach, - beforeAll: this._environment.global.beforeAll, - beforeEach: this._environment.global.beforeEach, - describe: this._environment.global.describe, - expect: this._environment.global.expect, - fdescribe: this._environment.global.fdescribe, - fit: this._environment.global.fit, - it: this._environment.global.it, - jest, - test: this._environment.global.test, - xdescribe: this._environment.global.xdescribe, - xit: this._environment.global.xit, - xtest: this._environment.global.xtest, - }; - } } function invariant(condition: unknown, message?: string): asserts condition { diff --git a/packages/jest-runtime/tsconfig.json b/packages/jest-runtime/tsconfig.json index 2955d779d6b1..08abc7a9ddfc 100644 --- a/packages/jest-runtime/tsconfig.json +++ b/packages/jest-runtime/tsconfig.json @@ -9,7 +9,6 @@ {"path": "../jest-console"}, {"path": "../jest-environment"}, {"path": "../jest-environment-node"}, - {"path": "../jest-globals"}, {"path": "../jest-haste-map"}, {"path": "../jest-message-util"}, {"path": "../jest-mock"},