-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Add src/setupTests.js to specify environment setup for Jest (#545) #548
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ You can find the most recent version of this guide [here](https://github.com/fac | |
- [Writing Tests](#writing-tests) | ||
- [Testing Components](#testing-components) | ||
- [Using Third Party Assertion Libraries](#using-third-party-assertion-libraries) | ||
- [Initializing Test Environment](#initializing-test-environment) | ||
- [Focusing and Excluding Tests](#focusing-and-excluding-tests) | ||
- [Coverage Reporting](#coverage-reporting) | ||
- [Continuous Integration](#continuous-integration) | ||
|
@@ -674,6 +675,24 @@ import { expect } from 'chai'; | |
|
||
and then use them in your tests like you normally do. | ||
|
||
### Initializing Test Environment | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add “>Note: this feature is available since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
>Note: this feature is available with `[email protected]` and higher. | ||
|
||
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a `src/setupTests.js` to your project. It will be automatically executed before running your tests. | ||
|
||
For example: | ||
|
||
#### `src/setupTests.js` | ||
```js | ||
const localStorageMock = { | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
clear: jest.fn() | ||
}; | ||
global.localStorage = localStorageMock | ||
``` | ||
|
||
### Focusing and Excluding Tests | ||
|
||
You can replace `it()` with `xit()` to temporarily exclude a test from being executed. | ||
|
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.
nit: you don't need the
: setupFiles
piece here because of object short notation.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.
I’m not sure Node 4 supports it, does it?
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.
True, wasn't sure of what es6 features I could use there since there are some "var" elsewhere in the code :s
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.
yes, it does! Node 4 supports let/var as well as object short notation and arrow functions.