From 635c17884150e881c834b87c1204770c1ae5e6ca Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Tue, 25 Jul 2023 14:26:49 -0400 Subject: [PATCH] fix: vitest types not working (closes #610) (rely on user provided types) --- types/index.d.ts | 7 ---- website/docs/getting-started/setup.md | 46 ++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 09fd8279..494221c6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -880,18 +880,11 @@ declare namespace jest { interface InverseAsymmetricMatchers extends Expect {} } -// removed since vitest 0.31.0. Useful for every vitest version before 0.31.0 declare namespace Vi { // eslint-disable-next-line @typescript-eslint/no-empty-interface interface AsymmetricMatchersContaining extends CustomMatchers {} } -// Changed since vitest 0.31.0. Useful for every vitest version after 0.31.0 -declare module 'vitest' { - // eslint-disable-next-line @typescript-eslint/no-empty-interface - interface AsymmetricMatchersContaining extends CustomMatchers {} -} - declare module 'jest-extended' { const matchers: CustomMatchers; export = matchers; diff --git a/website/docs/getting-started/setup.md b/website/docs/getting-started/setup.md index 0eb9e8d0..fe8c56c3 100644 --- a/website/docs/getting-started/setup.md +++ b/website/docs/getting-started/setup.md @@ -11,7 +11,6 @@ sidebar_position: 2 Create a setup script with the following: ```javascript title="testSetup.js" - // add all jest-extended matchers import * as matchers from 'jest-extended'; expect.extend(matchers); @@ -42,8 +41,8 @@ To automatically extend `expect` with all matchers, you can use `jest-extended` works with `vitest` because their `expect.extend` API is compatible. In your setup script: ```javascript title="testSetup.js" -import {expect} from "vitest"; -import * as matchers from "jest-extended"; +import { expect } from 'vitest'; +import * as matchers from 'jest-extended'; expect.extend(matchers); ``` @@ -52,7 +51,46 @@ Add this setup script to your `vitest.config.js`: ```javascript title="vitest.config.js" export default defineConfig({ test: { - setupFiles: ["./testSetup.js"], + setupFiles: ['./testSetup.js'], }, }); ``` + +### TypeScript types setup + +Create a file named _jest-extended.d.ts_ in your source directory with the content below. + +```typescript +export interface CustomMatchers extends Record { + toHaveBeenCalledAfter( + mock: jest.MockInstance | import('vitest').MockInstance, + failIfNoFirstInvocation: boolean, + ): R; + + toHaveBeenCalledBefore( + mock: jest.MockInstance | import('vitest').MockInstance, + failIfNoSecondInvocation: boolean, + ): R; + + toHaveBeenCalledExactlyOnceWith(...args: unknown[]): R; +} + +declare namespace jest { + interface Matchers { + toHaveBeenCalledAfter( + mock: jest.MockInstance | import('vitest').MockInstance, + failIfNoFirstInvocation?: boolean, + ): R; + + toHaveBeenCalledBefore( + mock: jest.MockInstance | import('vitest').MockInstance, + failIfNoSecondInvocation?: boolean, + ): R; + } +} + +import 'vitest'; +declare module 'vitest' { + interface Assertion extends CustomMatchers {} +} +```