Skip to content

Commit b3c129e

Browse files
committed
chore: roll new website version
1 parent 8f1c2e6 commit b3c129e

37 files changed

+9642
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
id: architecture
3+
title: Architecture
4+
---
5+
6+
If you are interested in learning more about how Jest works, understand its architecture, and how Jest is split up into individual reusable packages, check out this video:
7+
8+
<iframe width="560" height="315" src="https://www.youtube.com/embed/3YDiloj8_d0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
9+
10+
If you'd like to learn how to build a testing framework like Jest from scratch, check out this video:
11+
12+
<iframe width="560" height="315" src="https://www.youtube.com/embed/B8FbUK0WpVU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
13+
14+
There is also a [written guide you can follow](https://cpojer.net/posts/building-a-javascript-testing-framework). It teaches the fundamental concepts of Jest and explains how various parts of Jest can be used to compose a custom testing framework.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: bypassing-module-mocks
3+
title: Bypassing module mocks
4+
---
5+
6+
Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is calling functions from that module correctly. However, sometimes you may want to use parts of a mocked module in your _test file_, in which case you want to access the original implementation, rather than a mocked version.
7+
8+
Consider writing a test case for this `createUser` function:
9+
10+
```javascript
11+
// createUser.js
12+
import fetch from 'node-fetch';
13+
14+
export const createUser = async () => {
15+
const response = await fetch('http://website.com/users', {method: 'POST'});
16+
const userId = await response.text();
17+
return userId;
18+
};
19+
```
20+
21+
Your test will want to mock the `fetch` function so that we can be sure that it gets called without actually making the network request. However, you'll also need to mock the return value of `fetch` with a `Response` (wrapped in a `Promise`), as our function uses it to grab the created user's ID. So you might initially try writing a test like this:
22+
23+
```javascript
24+
jest.mock('node-fetch');
25+
26+
import fetch, {Response} from 'node-fetch';
27+
import {createUser} from './createUser';
28+
29+
test('createUser calls fetch with the right args and returns the user id', async () => {
30+
fetch.mockReturnValue(Promise.resolve(new Response('4')));
31+
32+
const userId = await createUser();
33+
34+
expect(fetch).toHaveBeenCalledTimes(1);
35+
expect(fetch).toHaveBeenCalledWith('http://website.com/users', {
36+
method: 'POST',
37+
});
38+
expect(userId).toBe('4');
39+
});
40+
```
41+
42+
However, if you ran that test you would find that the `createUser` function would fail, throwing the error: `TypeError: response.text is not a function`. This is because the `Response` class you've imported from `node-fetch` has been mocked (due to the `jest.mock` call at the top of the test file) so it no longer behaves the way it should.
43+
44+
To get around problems like this, Jest provides the `jest.requireActual` helper. To make the above test work, make the following change to the imports in the test file:
45+
46+
```javascript
47+
// BEFORE
48+
jest.mock('node-fetch');
49+
import fetch, {Response} from 'node-fetch';
50+
```
51+
52+
```javascript
53+
// AFTER
54+
jest.mock('node-fetch');
55+
import fetch from 'node-fetch';
56+
const {Response} = jest.requireActual('node-fetch');
57+
```
58+
59+
This allows your test file to import the actual `Response` object from `node-fetch`, rather than a mocked version. This means the test will now pass correctly.

0 commit comments

Comments
 (0)