Asymmetric Matchers in Vitest

From the Vitest cheat sheet ยท Assertions ยท verified Nov 2026

Asymmetric Matchers

Match parts of a value inside toEqual, toHaveBeenCalledWith, and snapshots

typescript
expect(user).toEqual({
  id: expect.any(Number),
  email: expect.stringMatching(/@example\.com$/),
  createdAt: expect.any(Date),
  roles: expect.arrayContaining(['admin']),
})

expect(fn).toHaveBeenCalledWith(expect.objectContaining({ id: 1 }))
๐Ÿ’ก Use asymmetric matchers for ids, dates and other changing values
โšก expect.schemaMatching validates with Zod, Valibot or ArkType
๐Ÿ“Œ They work inside toEqual, toHaveBeenCalledWith and snapshots
๐Ÿ” expect.not.* inverts a matcher, e.g. expect.not.arrayContaining
asymmetricexpect.anyobjectContaining

Continue with Vitest

Save the full cheat sheet or work through every related task.

More Vitest tasks

Back to the full Vitest cheat sheet