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

feat: add @jest/globals package for importing globals explicitly #9849

Merged
merged 7 commits into from Apr 22, 2020
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 @@ -2,6 +2,7 @@

### Features

- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801))
- `[jest-runtime]` Populate `require.cache` ([#9841](https://github.com/facebook/jest/pull/9841))

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion docs/GlobalAPI.md
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.
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'`.

## Methods

Expand Down
2 changes: 1 addition & 1 deletion docs/JestObjectAPI.md
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.
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'`.

## Mock Modules

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Expand Up @@ -2,7 +2,7 @@

exports[`on node >=12.16.0 runs test with native ESM 1`] = `
Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total
Tests: 10 passed, 10 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
Expand Down
13 changes: 13 additions & 0 deletions e2e/__tests__/importedGlobals.test.ts
@@ -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.
*/

import runJest from '../runJest';

test('imported globals', () => {
const result = runJest('imported-globals');
expect(result.exitCode).toBe(0);
});
22 changes: 22 additions & 0 deletions e2e/imported-globals/__tests__/env.test.js
@@ -0,0 +1,22 @@
/**
* 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);
});
8 changes: 8 additions & 0 deletions e2e/imported-globals/babel.config.js
@@ -0,0 +1,8 @@
/**
* 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');
5 changes: 5 additions & 0 deletions e2e/imported-globals/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
5 changes: 5 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Expand Up @@ -9,6 +9,7 @@ import {readFileSync} from 'fs';
import {createRequire} from 'module';
import {dirname, resolve} from 'path';
import {fileURLToPath} from 'url';
import {jest as jestObject} from '@jest/globals';
import staticImportedStateful from '../stateful.mjs';
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
import {double} from '../index';
Expand Down Expand Up @@ -90,3 +91,7 @@ test('handle unlinked dynamic imports', async () => {

expect(deepDouble(4)).toBe(8);
});

test('can import `jest` object', () => {
expect(jestObject).toBeDefined();
});
5 changes: 5 additions & 0 deletions packages/jest-globals/.npmignore
@@ -0,0 +1,5 @@
**/__mocks__/**
**/__tests__/**
src
tsconfig.json
tsconfig.tsbuildinfo
30 changes: 30 additions & 0 deletions packages/jest-globals/package.json
@@ -0,0 +1,30 @@
{
"name": "@jest/globals",
"version": "25.4.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.4.0",
"@jest/types": "^25.4.0",
"expect": "^25.4.0"
},
"publishConfig": {
"access": "public"
}
}
12 changes: 12 additions & 0 deletions packages/jest-globals/src/__tests__/index.ts
@@ -0,0 +1,12 @@
/**
* 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',
);
});
31 changes: 31 additions & 0 deletions packages/jest-globals/src/index.ts
@@ -0,0 +1,31 @@
/**
* 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',
);
14 changes: 14 additions & 0 deletions packages/jest-globals/tsconfig.json
@@ -0,0 +1,14 @@
{
"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"}
]
}
1 change: 1 addition & 0 deletions packages/jest-runtime/package.json
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@jest/console": "^25.4.0",
"@jest/environment": "^25.4.0",
"@jest/globals": "^25.4.0",
"@jest/source-map": "^25.2.6",
"@jest/test-result": "^25.4.0",
"@jest/transform": "^25.4.0",
Expand Down