Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Vitest Typescript documentation (closes #649) #662

Merged
merged 1 commit into from Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 42 additions & 18 deletions website/docs/getting-started/setup.md
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 {}
}
```