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
By making our test cases concurrent, we can switch from a test waterfall to a flat test execution:
14
14
15
-

15
+

16
16
17
-
Now that our test run at the same time, it is absolutely crucial we provision proper _test isolation_. We don't want tests to be stepping on each other's toes and becoming flaky as a result. This often comes down to eliminating or replacing any shared (or global) state in test cases.
17
+
Now that our tests run at the same time, it is absolutely crucial we provision proper _test isolation_. We don't want tests to be stepping on each other's toes and becoming flaky as a result. This often comes down to eliminating or replacing any shared (or global) state in test cases.
18
18
19
-
For example, the `expect()` function that you use to make assertions _might contain state_. It is essential we scoped that function to individual test case by accessing it from the test context object:
19
+
For example, the `expect()` function that you use to make assertions _might contain state_. It is essential we scope that function to individual test case by accessing it from the test context object:
20
20
21
21
```ts diff remove=1 add=3
22
22
test.concurrent(`${i}`, async () => {
@@ -55,7 +55,7 @@ export default defineConfig({
55
55
56
56
> 🦉 Bigger doesn't necessarily mean better with concurrency. There is a physical limit to any concurrency dictated by your hardware. If you set a `maxConcurrency` value higher than that limit, concurrent tests will be _queued_ until they have a slot to run.
57
57
58
-
By fine-tunning`maxConcurrency`, we are able to improve the test performance even further to the whooping 123ms!
58
+
By fine-tuning`maxConcurrency`, we are able to improve the test performance even further to the whopping 123ms!
59
59
60
60
```bashremove=1add=2
61
61
Duration 271ms
@@ -66,14 +66,14 @@ By fine-tunning `maxConcurrency`, we are able to improve the test performance ev
66
66
67
67
While concurrency may improve performance, it can also make your tests _flaky_. Keep in mind that the main price you pay for concurrency is _writing isolated tests_.
68
68
69
-
Here's a few guidelines on how to keep your tests concurrency-friendly:
69
+
Here are a few guidelines on how to keep your tests concurrency-friendly:
70
70
71
-
- **Do not rely on _shared state_ of any kind**. Never have multiple test modify the same data (even the `expect` function can become a shared state!). In practice, you achieve this through actions like:
71
+
- **Do not rely on _shared state_ of any kind**. Never have multiple tests modify the same data (even the `expect` function can become a shared state!). In practice, you achieve this through actions like:
72
72
- Striving toward self-contained tests (never have one test rely on the result of another);
73
73
- Keeping the test setup next to the test itself;
74
74
- Binding mocks (e.g. databases or network) to the test.
75
75
- **Isolate side effects**. If your test absolutely must perform a side effect, like writing to a file, guarantee that those side effects are isolated and bound to the test (e.g. create a temporary file accessed only by this particular test).
76
-
- **Abolish hard-coded values**. If two tests try to establish a mock server at the same port, one is destined to fail. Once again, procude test-specific fixtures (e.g. spawn a mock server at port `0` to get a random available port number).
76
+
- **Abolish hard-coded values**. If two tests try to establish a mock server at the same port, one is destined to fail. Once again, produce test-specific fixtures (e.g. spawn a mock server at port `0` to get a random available port number).
77
77
78
78
It is worth mentioning that due to these considerations, not all test cases can be flipped to concurrent and call it a day. Concurrency can, however, be a useful factor to stress your tests and highlight the shortcomings in their design. You can then address those shortcomings in planned, incremental refactors to benefit from concurrency (among other things) once your tests are properly isolated.
0 commit comments