|
6 | 6 |
|
7 | 7 | In this exercise, we will work on implementing simple unit tests using Jest.
|
8 | 8 |
|
9 |
| -## Jest Features |
| 9 | +## Jest features |
10 | 10 |
|
11 | 11 | - Multi-threaded and isolated test runner
|
12 |
| -- Provides a fake browser-like environment if needed (window, document, DOM, etc) using jsdom |
| 12 | +- Provides a fake browser-like environment if needed (window, document, DOM, etc) using [jsdom](https://github.com/jsdom/jsdom) |
13 | 13 | - Snapshots: Jest can create text-based snapshots of rendered components. These snapshots can be checked in and show API or large object changes alongside code changes in pull requests.
|
14 | 14 | - Code coverage is integrated (`--coverage`)
|
15 | 15 | - Very clear error messages showing where a test failure occurred
|
16 | 16 |
|
17 | 17 | ## How to use Jest
|
18 | 18 |
|
19 |
| -- Using `create-react-app` or other project generators, Jest should already be pre-configured. Running `npm test` usually will trigger it! |
20 |
| -- A `jest.config.js` file is used for configuration |
21 |
| -- `jsdom` might not have enough API from real browsers, for those cases, polyfills are required. Place these inside `jest.setup.js` and hook up the setup file in `jest.config.js` |
22 |
| -- in order to use `enzyme` library to test React Components, more config bits are needed inside `jest.setup.js` |
| 19 | +Using `create-react-app` or other project generators, Jest should already be pre-configured. Running `npm test` usually will trigger it! |
| 20 | + |
| 21 | +Setting up Jest in a new project is outside the scope of this course, but if you're interested in how it works, take a look at the bootcamp project's `jest.config.js` and `jest.setup.js` files or the [getting started documentation](https://jestjs.io/docs/en/getting-started). |
23 | 22 |
|
24 | 23 | ## What does a test look like?
|
25 | 24 |
|
26 | 25 | ```ts
|
27 |
| -// describe(), it() and expect() are globally exported, so they don't need to be imported when jest runs these tests |
| 26 | +// describe(), it() and expect() are globally exported, |
| 27 | +// so they don't need to be imported in each test file |
28 | 28 | describe('Something to be tested', () => {
|
29 | 29 | it('should describe the behavior', () => {
|
30 | 30 | expect(true).toBe(true);
|
31 | 31 | });
|
32 | 32 | });
|
33 | 33 | ```
|
34 | 34 |
|
| 35 | +- `describe()` takes a string describing the thing to be tested (often a component or file name) and a function which runs tests. |
| 36 | +- `it()` takes a string describing the behavior to be tested and a function to run the test. |
| 37 | +- `expect()` takes the actual value as a parameter and returns an object with various "matcher" methods to test against an expected value/condition. `toBe` is just one of [many available matchers](https://jestjs.io/docs/en/expect). |
| 38 | + |
| 39 | +> When choosing test names, think of the strings passed to `describe` and `it` as forming a sentence. For example, inside `describe('MyComponent', ...)` you might have a test `it('renders some text', ...)`, which forms the sentence a sentence describing the behavior: "MyComponent renders some text." |
| 40 | +
|
35 | 41 | ## Testing React components using Enzyme
|
36 | 42 |
|
37 | 43 | [Enzyme](https://airbnb.io/enzyme/) is made by Airbnb and provides utilities to help test React components.
|
@@ -88,7 +94,7 @@ it('some test function', () => {
|
88 | 94 |
|
89 | 95 | Read more about jest mocking [here](https://jestjs.io/docs/en/mock-functions.html).
|
90 | 96 |
|
91 |
| -### Async Testing |
| 97 | +### Async testing |
92 | 98 |
|
93 | 99 | For testing async scenarios, the test runner needs some way to know when the scenario is finished. Jest tests can handle async scenarios using callbacks, promises, or async/await.
|
94 | 100 |
|
|
0 commit comments