From bc75d104eb97c5cdf3d050aa4c53187580e174bd Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Mon, 9 Oct 2023 18:08:42 -0400 Subject: [PATCH] Update Vitest Typescript documentation (closes #649) (#662) Co-authored-by: Christopher Dierkens --- website/docs/getting-started/setup.md | 60 +++++++++++++++++++-------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/website/docs/getting-started/setup.md b/website/docs/getting-started/setup.md index 20e7f20b..e09e7a03 100644 --- a/website/docs/getting-started/setup.md +++ b/website/docs/getting-started/setup.md @@ -60,38 +60,62 @@ export default defineConfig({ To use Vitest with TypeScript, create a file named `jest-extended.d.ts` with the content below in addition to the setup above. +#### Vitest >= 0.31.0 + ```typescript -/// +import type CustomMatchers from 'jest-extended'; +import 'vitest'; + +declare module 'vitest' { + interface Assertion extends CustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers {} + interface ExpectStatic extends CustomMatchers {} +} +``` -export interface CustomMatchers extends Record { - toHaveBeenCalledAfter( - mock: jest.MockInstance | import('vitest').MockInstance, - failIfNoFirstInvocation?: boolean, - ): R; +This can be combined with matchers from other dependencies or your own custom matchers. Here's an example of a custom matcher. - toHaveBeenCalledBefore( - mock: jest.MockInstance | import('vitest').MockInstance, - failIfNoSecondInvocation?: boolean, - ): R; +```typescript +import type CustomMatchers from 'jest-extended'; +import 'vitest'; - toHaveBeenCalledExactlyOnceWith(...args: unknown[]): R; +interface MyCustomMatchers { + toBeFoo(): any; +} + +declare module 'vitest' { + interface Assertion extends CustomMatchers, MyCustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers, MyCustomMatchers {} + interface ExpectStatic extends CustomMatchers, MyCustomMatchers {} } ``` -For Vitest >= 0.31.0, append the content below to the new file. +#### Vitest < 0.31.0 ```typescript -declare module 'vitest' { - // eslint-disable-next-line @typescript-eslint/no-empty-interface +import type CustomMatchers from 'jest-extended'; +import 'vi'; + +declare module 'vi' { interface Assertion extends CustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers {} + interface ExpectStatic extends CustomMatchers {} } ``` -For Vitest < 0.31.0, append the content below to the new file. +This can be combined with matchers from other dependencies or your own custom matchers. Here's an example of a custom matcher. ```typescript -declare namespace Vi { - // eslint-disable-next-line @typescript-eslint/no-empty-interface - interface AsymmetricMatchersContaining extends CustomMatchers {} +import type CustomMatchers from 'jest-extended'; +import 'vi'; + +interface MyCustomMatchers { + toBeFoo(): any; +} + +declare module 'vi' { + interface Assertion extends CustomMatchers, MyCustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers, MyCustomMatchers {} + interface ExpectStatic extends CustomMatchers, MyCustomMatchers {} } ```