Asymmetric matchers

Loading "03 02 Problem"
Asymmetric matcher is the one where the actual value is literal while the expected value is an expression.
//     ๐Ÿ‘‡ Literal string
expect('hello world').toBe(expect.stringContaining('hello'))
//                         ๐Ÿ‘† Expression matching many strings
Above, the expect.stringContaining() matcher is asymmetric because it doesn't describe a literal value but instead creates what is, effectively, a regular expression that can match multiple strings (/hello/). It describes a logical equality, not structural.
Asymmetric matchers are fantastic for expectations that go beyond literal values.
Here are a few more examples of asymmetric matchers for you to consider:
// Must be an object containing the "id" property that is a string.
expect(user).toEqual(expect.objectContaining({ id: expect.any(String) }))

// Must be an array with exactly two elements that are numbers.
expect(caretPosition).toEqual([expect.any(Number), expect.any(Number)])
It is important to point out that in addition to asymmetric matchers all of my examples also include structural comparison: .toBe(), .toEqual(), etc. But instead of comparing the actual and expected values, it compares the actual value to the matcher result, which is what an asymmetric matcher returns.
This is what sets asymmetric matchers apart from symmetric matchers that don't involve literal values, like expect('hello').toMatch(/hello/).
In addition to this, asymmetric matchers are great for testing nested data structures as they allow you to describe expectations within the expected literal value:
expect(user).toEqual({
	id: 'abc-123',
	posts: expect.arrayContaining([
		expect.objectMatching({
			id: expect.any(String),
		}),
	]),
})
Here, the user object is expected to literally match the object with the id and posts properties. While the expectation toward the id property is literal, the posts property is described as an abstract Array<{ id: string }> object.

.toMatchSchema()

With that in mind, what kind of matcher is our custom .toMatchSchema()? ๐Ÿค”
It does accept a Zod schema, which is not a literal value we want to compare anything to. But on the other hand, it embodies the whole comparison, no matter if literal or not, instead of representing a matcher result:
expect('hello').toMatch(/hello/) // symmetric
expect(user).toMatchSchema(userSchema) // also symmetric

expect('hello').toEqual(expect.stringMatching(/hello/)) // asymmetric
expect(user).toEqual(expect.toMatchSchema(userSchema)) // ???
Wait, can we even use it as an asymmetric matcher? Let's find out:
import { fetchUser } from './fetch-user'
import { userSchema } from './schemas'

test('returns the user by id', async () => {
	const user = await fetchUser('abc-123')
	expect(user).toMatchSchema(userSchema)
	expect(user).toEqual(expect.toMatchSchema(userSchema))
})
npm test

 โœ“ src/fetch-user.test.ts (1 test) 2ms
   โœ“ returns the user by id 1ms
Somehow, that assertion also passes! ๐Ÿ˜ฎ
That is happening because Vitest automatically treats custom matchers as both symmetric and asymmetric, allowing you to implement them just once and use them as you see fit.
The .toMatchSchema() matcher is both symmetric and asymmetric depending on how it's being used.
There is a slight problem though... Types.
test('returns the user by id', async () => {
	const user = await fetchUser('abc-123')
	expect(user).toEqual(expect.toMatchSchema(userSchema))
	// โŒ                        ^^^^^^^^^^^^^
	// Property 'toMatchSchema' does not exist on type 'ExpectStatic'.ts(2339)
})
At the moment of writing this exercise, Vitest does not extend the asymmetric matchers interface to let TypeScript know what type expect.toMatchSchema() is. But you know who will?

Your task

๐Ÿ‘จโ€๐Ÿ’ผ You! Your task right now is to modify the module augmentation in so that asymmetric matchers are recognized on the type level. Since the tests are passing as-is, you will use your IDE to verify that the custom .toMatchSchema() matcher has correct type definitions (use the modified for that).
๐Ÿ‘จโ€๐Ÿ’ผ Once the type story is solved, I want to you give the asymmetric matchers a try. In the , you will find an unfinished test case. Complete it using the asymmetric expect.toMatchSchema() matcher and have it passing!

Please set the playground first

Loading "Asymmetric matchers"
Loading "Asymmetric matchers"
Login to get access to the exclusive discord channel.
  • general
    Modals / Dialogs
    Lucas Wargha ๐Ÿš€ ๐ŸŒŒ:
    It seems like modals and dialogs are becoming a hot topic on my team lately. I havenโ€™t found a solid...
    • โœ…1
    3 ยท 3 months ago
  • general
    Welcome to EpicWeb.dev! Say Hello ๐Ÿ‘‹
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒโšก:
    This is the first post of many hopefully!
    • 18
    86 ยท 2 years ago
  • general
    epic stack website initial load at home page is unstyled (sometimes)
    osmancakir ๐Ÿš€ ๐ŸŒŒ:
    Sometimes (especially when it is loaded first time on a new browser etc.) I see this unstyled versio...
    • โœ…1
    10 ยท 6 months ago
  • general
    Resource / Api endpoints on epic stack / RR7
    Lucas Wargha ๐Ÿš€ ๐ŸŒŒ:
    Hi everyone! Quick question for those using the Epic Stack: How are you handling resource routes ...
    • โœ…1
    2 ยท 5 months ago
  • general
    Epic stack using tanstack form
    Lucas Wargha ๐Ÿš€ ๐ŸŒŒ:
    https://github.com/epicweb-dev/epic-stack/compare/epicweb-dev:main...wargha:feature/tanstack-form-ex...
    • โœ…1
    3 ยท 5 months ago
  • general
    Init command outdated on the EpicWeb website
    Virgile ๐Ÿ† ๐ŸŒŒ:
    Hi everyone. I've initialized a new epic-stack project yesterday. Following instructions from http...
    • โœ…1
    3 ยท 5 months ago
  • general
    Mark as complete, resets the first time you click it.
    Daniel V.C ๐Ÿš€ ๐ŸŒŒ:
    Not sure if anyone else has had this issue, as i've not seen anyone else talk about it, but I find ...
    • โœ…1
    8 ยท 6 months ago
  • ๐Ÿ’พdata
    general
    ๐Ÿ“forms
    ๐Ÿ”ญfoundations
    double underscore?
    trendaaang ๐ŸŒŒ:
    What with the `__note-editor.tsx`? I don't see that in the Remix docs and I don't remember Kent talk...
    • โœ…1
    2 ยท a year ago
  • general
    Keeping Epic Stack Projects Free on Fly โ€“ Any Tips?
    Lucas Wargha ๐Ÿš€ ๐ŸŒŒ:
    Iโ€™ve been experimenting with the Epic Stack and deploying some dummy projects on Fly. I noticed that...
    • โœ…1
    0 ยท 6 months ago
  • ๐Ÿ’พdata
    general
    ๐Ÿ“forms
    ๐Ÿ”ญfoundations
    Creating Notes
    Scott ๐ŸŒŒ ๐Ÿ†:
    Does anybody know in what workshop we create notes? I would like to see the routing structure. So fa...
    • โœ…1
    2 ยท 8 months ago
  • ๐Ÿ”ญfoundations
    ๐Ÿ’พdata
    general
    ๐Ÿ“forms
    ๐Ÿ”auth
    Thank you for the inspiration
    Binalfew ๐Ÿš€ ๐ŸŒŒ โšก:
    <@105755735731781632> I wanted to thank you for the incredible knowledge I gained from your Epic Web...
    • โค๏ธ1
    1 ยท 8 months ago
  • general
    npm install everytime I setup a new playground
    Duki ๐ŸŒŒ:
    Is it normal that I have to run `npm install` in my playground directory, everytime I setup the play...
    • โœ…1
    2 ยท a year ago
  • general
    Migration to Vite: Server-only module referenced by client
    Fabian ๐ŸŒŒ:
    Hi, I'm working on migrating to Vite following the remix docs (https://remix.run/docs/en/main/guides...
    • โœ…1
    1 ยท a year ago
  • general
    Remix Vite Plugin
    Binalfew ๐Ÿš€ ๐ŸŒŒ โšก:
    <@105755735731781632> Now that remix officially supports vite (though not stable) what does it mean...
    • โœ…1
    3 ยท 2 years ago
  • general
    ๐Ÿ”ญfoundations
    Solutions video on localhost:5639 ?
    quang ๐Ÿš€ ๐ŸŒŒ:
    Hi, so I'm having a hard time navigating (hopefully will be better with time) The nav on epicweb.de...
    • โœ…1
    9 ยท 2 years ago
  • general
    Epicshop is now social and mobile friendly!
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒโšก:
    I'm excited to announce that now the Epic Web workshops are mobile friendly! https://foundations.ep...
    • ๐ŸŽ‰2
    0 ยท a year ago