Skip to content

Commit 4db00a0

Browse files
committed
02/01: approach solution usage-first
1 parent ea5e76b commit 4db00a0

File tree

1 file changed

+15
-10
lines changed
  • exercises/02.context/01.solution.custom-fixtures

1 file changed

+15
-10
lines changed

exercises/02.context/01.solution.custom-fixtures/README.mdx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
Once I've got `@faker-js/faker` installed, I will head to a newly created `test-extend.ts` module and implement my custom fixture.
44

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.
66

7-
```ts filename=test-extend.ts add=1-3
8-
import { test as testBase } 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:
98

10-
export const test = testBase.extend({})
9+
```ts filename=test-extend.ts add=1-3
10+
interface Fixtures {
11+
createMockCart: (items: Array<Partial<CartItem>>) => Cart
12+
}
1113
```
1214

13-
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.
1416

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:
1620

17-
```ts filename=test-extend.ts add=2,4-6 highlight=8
21+
```ts filename=test-extend.ts add=1,7
1822
import { test as testBase } from 'vitest'
19-
import type { Cart, CartItem } from './src/cart-utils'
2023

2124
interface Fixtures {
2225
createMockCart: (items: Array<Partial<CartItem>>) => Cart
@@ -25,9 +28,11 @@ interface Fixtures {
2528
export const test = testBase.extend<Fixtures>({})
2629
```
2730

28-
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.
2932
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.
3136

3237
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:
3338

0 commit comments

Comments
 (0)