diff --git a/.changeset/three-buckets-rush.md b/.changeset/three-buckets-rush.md new file mode 100644 index 00000000..527fec26 --- /dev/null +++ b/.changeset/three-buckets-rush.md @@ -0,0 +1,5 @@ +--- +'jest-extended': patch +--- + +Remove problematic Vitest types diff --git a/types/index.d.ts b/types/index.d.ts index 09fd8279..8c1018bf 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -880,18 +880,6 @@ 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..20e7f20b 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,47 @@ Add this setup script to your `vitest.config.js`: ```javascript title="vitest.config.js" export default defineConfig({ test: { - setupFiles: ["./testSetup.js"], + setupFiles: ['./testSetup.js'], }, }); ``` + +### Vitest TypeScript types setup + +To use Vitest with TypeScript, create a file named `jest-extended.d.ts` with the content below in addition to the setup above. + +```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; +} +``` + +For Vitest >= 0.31.0, append the content below to the new file. + +```typescript +declare module 'vitest' { + // eslint-disable-next-line @typescript-eslint/no-empty-interface + interface Assertion extends CustomMatchers {} +} +``` + +For Vitest < 0.31.0, append the content below to the new file. + +```typescript +declare namespace Vi { + // eslint-disable-next-line @typescript-eslint/no-empty-interface + interface AsymmetricMatchersContaining extends CustomMatchers {} +} +```