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: exercises/02.context/01.solution.custom-fixtures/README.mdx
+15-10Lines changed: 15 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,24 @@
2
2
3
3
Once I've got `@faker-js/faker` installed, I will head to a newly created `test-extend.ts` module and implement my custom fixture.
4
4
5
-
Custom fixtures are enabled through creating your own `test()` function in Vitest by calling `test.extend()`:
5
+
When it comes to any API design, I like approaching it _usage-first_. I create the usage experience I want and then deal with the implementation details to bring that vision to life. This custom fixture is no exception.
6
6
7
-
```ts filename=test-extend.ts add=1-3
8
-
import { testastestBase } from'vitest'
7
+
In `test-extend.ts`, I will define a new type called `Fixtures` and use it to describe my custom fixture's name and its call signature:
The `test.extend()`function accepts an object where each key represents a custom fixture and each value that respective fixture's value.
15
+
Above, I've declared a single custom fixture called `createMockCart`, which is a function that accepts an array of cart `items` and returns a cart object. I plan on using that fixture to help me mock different states of the cart in tests.
14
16
15
-
I'm going to approach this type-first, declaring the expected type of my fixtures in a new interface called `Fixtures` and providing it as a type argument to the `test.extend()` call:
17
+
Now it's time to implement it.
18
+
19
+
Custom fixtures in Vitest work by extending the default `test()` function using the `test.extend()` method:
Above, I've declared a single custom fixture called `createMockCart`, which is a function that accepts an array of cart `items` and returns a cart object. I plan on using that fixture to help me mock different states of the cart object in tests.
31
+
> I am importing the `test`function under the `testBase` alias to prevent name collision with my custom `test` function.
29
32
30
-
Now it's time to implement it.
33
+
The `test.extend()` method accepts an object that represents my custom fixtures. By providing the `Fixtures` type as the type argument to that method, I am letting Vitest know about my fixtures' type definitions.
34
+
35
+
Let's add the implementation for the `createMockCart()` fixture.
31
36
32
37
Despite the call signature of my fixture making it a simple function, the actual implementation for it will be a bit different. You can imagine the fixture implementation as a _higher-order function_ that allow you to hook into Vitest's existing context and internals, as well as control any side effects required to run _before_ and _after_ your fixture:
0 commit comments