Skip to content

Commit

Permalink
Update Vitest Typescript documentation (closes #649) (#662)
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher Dierkens <[email protected]>
  • Loading branch information
keeganwitt and cdierkens authored Oct 9, 2023
1 parent 1f88101 commit bc75d10
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions website/docs/getting-started/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <reference types="vitest" />
import type CustomMatchers from 'jest-extended';
import 'vitest';

declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T> {}
interface ExpectStatic extends CustomMatchers<T> {}
}
```

export interface CustomMatchers<R> extends Record<string, any> {
toHaveBeenCalledAfter(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
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<any, any[]> | import('vitest').MockInstance<any, any[]>,
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<T = any> extends CustomMatchers<T>, MyCustomMatchers {}
interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T>, 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<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T> {}
interface ExpectStatic extends CustomMatchers<T> {}
}
```

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<any> {}
import type CustomMatchers from 'jest-extended';
import 'vi';

interface MyCustomMatchers {
toBeFoo(): any;
}

declare module 'vi' {
interface Assertion<T = any> extends CustomMatchers<T>, MyCustomMatchers {}
interface AsymmetricMatchersContaining<T = any> extends CustomMatchers<T>, MyCustomMatchers {}
interface ExpectStatic extends CustomMatchers, MyCustomMatchers {}
}
```

0 comments on commit bc75d10

Please sign in to comment.