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

Allow importing Jest as an ES6 module #7571

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest]` Export globals as an ES6 module ([./docs/Es6Import.md](./docs/Es6Import.md)).
- `[jest-runtime]` Add `jest.isolateModules` for scoped module initialization ([#6701](https://github.com/facebook/jest/pull/6701))
- `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363))
- `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119))
Expand Down
86 changes: 86 additions & 0 deletions docs/Es6Import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
id: es6-import
title: ES6 Import
---

If you prefer the [Ava](https://ava.li/) approach of no polluting the global environment, Jest now supports importing as an ES6 module to avoid refering to global variables.

This is completely optional. Everything is still usable the same as before. But if you have a code editor that complains when you use unrecognized globals, or you just prefer to use the ES6 module approach, you can now import anything that is already defined as a global.

## Example

Consider the following example:

```js
test('it works', () => {
expect('Hello, World!').toHaveLength(13);
});
```

This can now be rewritten as:

```js
import jest from 'jest';

jest.test('it works', () => {
jest.expect('Hello, World!').toHaveLength(13);
});
```

## Reference

```js
import jest from 'jest';
```

The `jest` object now contains the following properties:

```
{
expect
afterAll
afterEach
beforeAll
beforeEach
describe
describe.each
describe.only
describe.only.each
describe.skip
describe.skip.each
test
test.each
test.only
test.only.each
test.skip
test.skip.each
clearAllTimers
disableAutomock
enableAutomock
fn
isMockFunction
genMockFromModule
mock
unmock
doMock
dontMock
clearAllMocks
resetAllMocks
restoreAllMocks
resetModules
retryTimes
runAllTicks
runAllTimers
advanceTimersByTime
runOnlyPendingTimers
requireActual
requireMock
setMock
setTimeout
useFakeTimers
useRealTimers
spyOn
}
```

See [Expect API](ExpectAPI.md), [Global API](GlobalAPI.md), and [Jest Object API](JestObjectAPI.md) for more info.
2 changes: 2 additions & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ PASS ./sum.test.js

This test used `expect` and `toBe` to test that two values were exactly identical. To learn about the other things that Jest can test, see [Using Matchers](UsingMatchers.md).

If you prefer to import functions instead of using them globally, see [ES6 Import](Es6Import.md).

## Running from command line

You can run Jest directly from the CLI (if it's globally available in your `PATH`, e.g. by `yarn global add jest` or `npm install jest --global`) with a variety of useful options.
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.
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 (though they still can be [imported](Es6Import.md) if you prefer).

## 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.
The `jest` object is automatically in scope within every test file (although it can also be imported as an [ES6 module](Es6Import.md)). The methods in the `jest` object help create mocks and let you control Jest's overall behavior.

## Mock Modules

Expand Down
19 changes: 19 additions & 0 deletions packages/jest/src/__tests__/jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.

import jest from '../jest';

const sum = (a, b) => a + b;

describe('jest es6 import', () => {
jest.describe('jest.describe', () => {
jest.test('should work like global describe', () => {
jest.expect(sum(1, 2)).toBe(3);
});
});

jest.xdescribe('jest.xdescribe', () => {
jest.test('should be skipped just like global describe.skip', () => {
jest.expect(sum(1, 2)).toBe(3);
});
});
});
18 changes: 17 additions & 1 deletion packages/jest/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@
* @flow
*/

export {default} from 'jest-cli';
import cli from 'jest-cli';

const obj = {
...jest,
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
fdescribe: describe.only,
it: test,
test,
xdescribe: describe.skip,
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be missing fit, xit, and xtest. (full list is at https://jestjs.io/docs/en/api)


export default (process.env.JEST_WORKER_ID !== undefined ? obj : cli);