I wrote a custom matcher with expect.extend
expect.extend({
toEqualIgnoringWhitespace(got: string, want: string) {
return {
pass: got.trim().replace(/s+/g, ``) === want.trim().replace(/s+/g, ``),
message: () => `Even with whitespace removed, ${got} does not equal ${want}.`
};
}
});
And as recommended by the docs I added a vitest.d.env
interface CustomMatchers<R = unknown> {
toEqualIgnoringWhitespace: () => R
}
declare module 'vitest' {
type Assertion<T = any> = CustomMatchers<T>
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
And I followed the docs:
Don’t forget to include the ambient declaration file in your tsconfig.json.
"include": ["vitest.d.ts","env.d.ts", "**/*.ts", "**/*.tsx"],
But now Typescript doesn’t know about the rest of vitest. Using test.extend
, use
and task
implicitly have any
type.
const it = test.extend<{ row: EventRow }>({
row: async ({ task }, use) => { // errors here
...
It got clearer when I tried to import stuff from vitest
and TS didn’t recognize of the normal things like expect
(I’m using globals for describe
, it
, etc; I tried these imports as a test). it only suggests the two things from the .d.env
I pasted from the docs.
So it looks like I’m doing something wrong in either the tsconfig or the .d.env file but I don’t know how to fix it.