You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: apps/site/pages/en/learn/test-runner/mocking.md
+14-16
Original file line number
Diff line number
Diff line change
@@ -21,19 +21,19 @@ But first, there are several types of tests:
21
21
| integration | components fitting together | - | external code, external system |
22
22
| end-to-end (e2e) | app + external data stores, delivery, etc | A fake user (ex a Playwright agent) literally using an app connected to real external systems. | none (do not mock) |
23
23
24
-
There are varying schools of thought about when you should and should not mock, the broadstrokes of which are laid out below.
24
+
There are different schools of thought about when to mock and when not to mock, the broad strokes of which are outlined below.
25
25
26
26
## When and not to mock
27
27
28
28
There are 3 main mock candidates:
29
29
30
-
-own code
31
-
-external code
32
-
-external system
30
+
-Own code
31
+
-External code
32
+
-External system
33
33
34
34
### Own code
35
35
36
-
This is what you/your project control.
36
+
This is what your project controls.
37
37
38
38
```mjs displayName="your-project/main.mjs"
39
39
importfoofrom'./foo.mjs';
@@ -53,11 +53,11 @@ For a true unit test of `main`, `foo` should be mocked: you're testing that `mai
53
53
54
54
Mocking `foo` can be more trouble than worth, especially when `foo` is simple, well-tested, and rarely updated.
55
55
56
-
Others argue that not mocking `foo`is better because it's more authentic and increases coverage of `foo` (because `main`'s tests will also verify `foo`). This can, however, create noise: when `foo` breaks, a bunch of other tests will also break, so tracking down the problem is more tedious: if only the 1 test for the item ultimately responsible for the issue is failing, that's very easy to spot; whereas 100 tests failing creates a needle-in-a-haystack to find the real problem.
56
+
Not mocking `foo`can be better because it's more authentic and increases coverage of `foo` (because `main`'s tests will also verify `foo`). This can, however, create noise: when `foo` breaks, a bunch of other tests will also break, so tracking down the problem is more tedious: if only the 1 test for the item ultimately responsible for the issue is failing, that's very easy to spot; whereas 100 tests failing creates a needle-in-a-haystack to find the real problem.
57
57
58
58
### External code
59
59
60
-
This is what you/your project does not control.
60
+
This is what your project does not control.
61
61
62
62
```mjs displayName="your-project/main.mjs"
63
63
importbarfrom'bar';
@@ -73,11 +73,11 @@ Uncontroversially, for unit tests, this should always be mocked. For component a
73
73
74
74
#### Why
75
75
76
-
Verifying that someone else's code works with your code is not the role of a unit test (and their code should already be tested).
76
+
Verifying that code that your project does not maintain works is not the goal of a unit test (and that code should have its own tests).
77
77
78
78
#### Why not
79
79
80
-
Sometimes, it's just not realistic to mock. For example, you would almost never mock react (the medicine would be worse than the ailment).
80
+
Sometimes, it's just not realistic to mock. For example, you would almost never mock a large framework such as react or angular (the medicine would be worse than the ailment).
In the above, the first and second cases (the `it`s) can sabotage each other because they are run concurrently and mutate the same store (a race condition): "save"'s insertion can cause the otherwise valid "read"'s test to fail its assertion on items found (and "read"'s can do the same thing to "save"'s).
138
+
In the above, the first and second cases (the `it()` statements) can sabotage each other because they are run concurrently and mutate the same store (a race condition): `save()`'s insertion can cause the otherwise valid `read()`'s test to fail its assertion on items found (and `read()`'s can do the same thing to `save()`'s).
139
139
140
140
## What to mock
141
141
142
142
### Modules + units
143
143
144
-
This leverages [`mock`](https://nodejs.org/api/test.html#class-mocktracker) from node's test runner.
144
+
This leverages [`mock`](https://nodejs.org/api/test.html#class-mocktracker) from the Node.js test runner.
A little-known fact: node has a builtin way to mock `fetch`. [`undici`](https://github.com/nodejs/undici) is the Node.js implementation of `fetch`. You do not have to install it—it's shipped with `node` by default.
183
+
A little-known fact is that there is a builtin way to mock `fetch`. [`undici`](https://github.com/nodejs/undici) is the Node.js implementation of `fetch`. You do not have to install it—it's shipped with `node` by default.
Like Doctor Strange, you too can control time. You would usually do this just for convience to avoid artificially protracted test runs (do you really want to wait 3 minutes for that setTimeout to trigger?). You may also want to travel through time. This leverages [`mocktimers`](https://nodejs.org/api/test.html#class-mocktimers) from node's test runner.
247
+
Like Doctor Strange, you too can control time. You would usually do this just for convenience to avoid artificially protracted test runs (do you really want to wait 3 minutes for that `setTimeout()` to trigger?). You may also want to travel through time. This leverages [`mock.timers`](https://nodejs.org/api/test.html#class-mocktimers) from the Node.js test runner.
250
248
251
249
Note the use of time-zone here (`Z` in the time-stamps). Neglecting to include a consistent time-zone can (read: likely will) lead to unexpected restults.
0 commit comments