Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
Revert "feat: add @jest/globals package for importing globals expli…
Browse files Browse the repository at this point in the history
…citly (jestjs#9801)"

This reverts commit 2882d28.
  • Loading branch information
SimenB committed Apr 19, 2020
1 parent 3681cca commit 75ab1ab
Show file tree
Hide file tree
Showing 15 changed files with 7 additions and 191 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/GlobalAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 0 additions & 13 deletions e2e/__tests__/importedGlobals.test.ts

This file was deleted.

22 changes: 0 additions & 22 deletions e2e/imported-globals/__tests__/env.test.js

This file was deleted.

8 changes: 0 additions & 8 deletions e2e/imported-globals/babel.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions e2e/imported-globals/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/jest-globals/.npmignore

This file was deleted.

30 changes: 0 additions & 30 deletions packages/jest-globals/package.json

This file was deleted.

12 changes: 0 additions & 12 deletions packages/jest-globals/src/__tests__/index.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/jest-globals/src/index.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/jest-globals/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 5 additions & 46 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ 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,
LocalModuleRequire,
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';
Expand All @@ -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;
Expand Down Expand Up @@ -152,7 +146,6 @@ class Runtime {
private _unmockList: RegExp | undefined;
private _virtualMocks: BooleanObject;
private _moduleImplementation?: typeof nativeModule.Module;
private jestObjectCaches: Map<string, Jest>;

constructor(
config: Config.ProjectConfig,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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];
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion packages/jest-runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 75ab1ab

Please sign in to comment.