From 4d1b5877c86987108774ce82f4a2384b6a1b9d52 Mon Sep 17 00:00:00 2001 From: Mordechai Dror Date: Sun, 14 Jul 2024 12:04:09 +0300 Subject: [PATCH] improve extending matchers docs - add satisfies keyword as it improves types - change ambient declaration file to how it is configured inside [testing lib](https://github.com/testing-library/jest-dom/blob/main/types/vitest.d.ts) since the current way didn't work for me - rename MatcherResult to ExpectationResult as this is the current name of custom matcher return type according to source code (see MatchersObject > RawMatcherFn > ExpectationResult) --- docs/guide/extending-matchers.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/guide/extending-matchers.md b/docs/guide/extending-matchers.md index 1c650cc856a78..10fb2c7ff4945 100644 --- a/docs/guide/extending-matchers.md +++ b/docs/guide/extending-matchers.md @@ -11,6 +11,8 @@ This guide will explore extending matchers with `expect.extend`. If you are inte To extend default matchers, call `expect.extend` with an object containing your matchers. ```ts +import type { MatchersObject } from '@vitest/expect' + expect.extend({ toBeFoo(received, expected) { const { isNot } = this @@ -20,19 +22,17 @@ expect.extend({ message: () => `${received} is${isNot ? ' not' : ''} foo` } } -}) +} satisfies MatchersObject) ``` If you are using TypeScript, you can extend default `Assertion` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below: ```ts -import type { Assertion, AsymmetricMatchersContaining } from 'vitest' - interface CustomMatchers { toBeFoo: () => R } -declare module 'vitest' { +declare module '@vitest/expect' { interface Assertion extends CustomMatchers {} interface AsymmetricMatchersContaining extends CustomMatchers {} } @@ -45,7 +45,7 @@ Don't forget to include the ambient declaration file in your `tsconfig.json`. The return value of a matcher should be compatible with the following interface: ```ts -interface MatcherResult { +interface ExpectationResult { pass: boolean message: () => string // If you pass these, they will automatically appear inside a diff when