Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 852b534

Browse files
committed
Jest step updates
1 parent bdb1710 commit 852b534

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

bonus-jest/demo/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@
66

77
In this exercise, we will work on implementing simple unit tests using Jest.
88

9-
## Jest Features
9+
## Jest features
1010

1111
- 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)
1313
- 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.
1414
- Code coverage is integrated (`--coverage`)
1515
- Very clear error messages showing where a test failure occurred
1616

1717
## How to use Jest
1818

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).
2322

2423
## What does a test look like?
2524

2625
```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
2828
describe('Something to be tested', () => {
2929
it('should describe the behavior', () => {
3030
expect(true).toBe(true);
3131
});
3232
});
3333
```
3434

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+
3541
## Testing React components using Enzyme
3642

3743
[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', () => {
8894

8995
Read more about jest mocking [here](https://jestjs.io/docs/en/mock-functions.html).
9096

91-
### Async Testing
97+
### Async testing
9298

9399
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.
94100

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ <h2>Bonus Content</h2>
145145
</li>
146146
<li class="Tile Tile--numbered">
147147
<div class="Tile-link">
148-
jest: testing code
148+
Testing with Jest
149149
<div class="Tile-links">
150150
<a target="_blank" href="./bonus-jest/demo/">demo</a> | <a target="_blank" href="./bonus-jest/exercise/">exercise</a>
151151
</div>

0 commit comments

Comments
 (0)