-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
, andxtest
. (full list is at https://jestjs.io/docs/en/api)