Custom Matchers in Vitest
From the Vitest cheat sheet · Assertions · verified Nov 2026
Custom Matchers
Add your own matchers with expect.extend and type them for Vitest 5
typescript
expect.extend({
toBeWithinRange(received: number, floor: number, ceiling: number) {
return {
pass: received >= floor && received <= ceiling,
message: () => `expected ${received} to be within ${floor}-${ceiling}`,
}
},
})
expect(5).toBeWithinRange(1, 10)📌 v5 changed the type to Matchers<R, T>; return R from sync matchers
💡 One interface types expect().x, expect.x and expect.extend
⚠️ Vitest 5 no longer reads jest.Matchers declarations
🟢 Register matchers in a setup file so every test file gets them
expect.extendcustom-matcherstypescript
Continue with Vitest
Save the full cheat sheet or work through every related task.