Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"devDependencies": {
"@edge-runtime/types": "^4.0.0",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"@edge-runtime/types": "^4.0.0",
"@edge-runtime/vm": "^5.0.0",
"edge-runtime": "^4.0.1",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
2 changes: 1 addition & 1 deletion exercises/01.setup/03.problem.code-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"devDependencies": {
"@vitest/ui": "^3.1.1",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
2 changes: 1 addition & 1 deletion exercises/01.setup/03.solution.code-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"devDependencies": {
"@vitest/coverage-v8": "^3.1.1",
"@vitest/ui": "^3.1.1",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"devDependencies": {
"@faker-js/faker": "^9.7.0",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
},
"dependencies": {
"sqlite3": "^5.1.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
},
"dependencies": {
"sqlite3": "^5.1.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@
// 🐨 Augment the type definition for the `vitest` module.
// 💰 declare module 'name' {}
//
// 🐨 Inside the module declaration, declare an interface called `Assertion<T = any>`.
// 🐨 Inside the module declaration, declare an interface called `Matchers<T>`.
// Make it extend the `CustomMatchers` interface you created earlier.
// This will extend the type definitions of the `expect()` function to recognize
// your custom matchers.
// 💰 interface Assertion<T = any> extends CustomMatchers<T> {}
//
// 🐨 In a similar way, declare another interface called `MatchersDeclaration`
// and make it extend your `CustomMatchers` interface.
// This will extend the type definitions of the `expect.extend()` function
// to make the implementation of your custom matchers type-safe.
// 💰 interface MatchersDeclaration extends CustomMatchers {}
// 💰 interface Matchers<T> extends CustomMatchers<T> {}
//
// This covers your custom matcher on the type-level.
// Now, let's continue with its implementation!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ But this is only half of the story. Running this `expect()` statement will throw

To provide a matcher implementation, call `expect.extend()` and provide it with an object that contains the definitions for your custom matchers:

```ts filename=vitest.setup.ts add=1,13-15
```ts filename=vitest.setup.ts add=1,12-14
import { expect } from 'vitest'
import type { Schema } from 'zod'

Expand All @@ -59,8 +59,7 @@ interface CustomMatchers<MatcherResult = any> {
}

declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface MatchersDeclaration extends CustomMatchers {}
interface Matchers<T> extends CustomMatchers<T> {}
}

expect.extend({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"devDependencies": {
"@types/node": "^24.3.0",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
},
"dependencies": {
"zod": "^3.24.2"
Expand Down
20 changes: 4 additions & 16 deletions exercises/03.assertions/02.solution.asymmetric-matchers/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@

<EpicVideo url="https://www.epicweb.dev/workshops/advanced-vitest-patterns/assertions/03-02-problem/solution" />

To expose my `.toMatchSchema()` matcher as an asymmetric matcher, I will extend the `AsymmetricMatchersContaining` interface of the `vitest` module:

```ts nonumber add=4
declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface MatchersDeclaration extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
```

> The `AsymmetricMatchersContaining` controls the matcher types available as asymmetric matchers on the `expect` object (e.g. `expect.objectContaining()`).
To expose my `.toMatchSchema()` matcher as an asymmetric matcher, I need to... do nothing! Vitest automatically provides both symmetric and asymmetric versions for your custom matchers, including type definitions.

With this change, the type error in the `src/fetch-user.test.ts` is finally gone! 🎉
All that's left for me to do is to use the asymmetric version of the `toMatchSchema()` matcher in tests:

```ts filename=src/fetch-user.test.ts highlight=6
import { fetchUser } from './fetch-user'
Expand All @@ -26,9 +16,7 @@ test('returns the user by id', async () => {
})
```

This means that I can use the `.toMatchSchema()` matcher as asymmetric, which is exactly what I need to complete the test case for user transactions.

In `src/fetch-transaction.test.ts`, I will write the remaining test:
And in `src/fetch-transaction.test.ts`, I will write the remaining test:

```ts filename=src/fetch-transaction.test.ts add=5-11
import { fetchTransaction, type Transaction } from './fetch-transaction'
Expand All @@ -45,7 +33,7 @@ test('fetches a transaction between two users', async () => {
})
```

> 🦉 Notice how I'm using schema validation to make sure that transaction's `issuer` and `recipient` properties both match the `userSchema`.
> 🦉 Notice the usage of the `Transaction` type as the type argument to the default `.toEqual` matcher. It ensures that the expected value I pass to that matcher is type-safe.

## Related materials

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"devDependencies": {
"@types/node": "^24.3.0",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
},
"dependencies": {
"zod": "^3.24.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"test": "vitest --no-cache"
},
"devDependencies": {
"vitest": "^3.1.1",
"vitest": "^3.2.4",
"vitest-profiler": "^0.1.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test:profile": "vitest-profiler npm test"
},
"devDependencies": {
"vitest": "^3.1.1",
"vitest": "^3.2.4",
"vitest-profiler": "^0.1.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"devDependencies": {
"@types/node": "^24.3.0",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"devDependencies": {
"@types/node": "^24.3.0",
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"test": "vitest"
},
"devDependencies": {
"vitest": "^3.1.1"
"vitest": "^3.2.4"
}
}
Loading
Loading