Skip to content

Commit

Permalink
fix: vitest types not working (closes #610)
Browse files Browse the repository at this point in the history
(rely on user provided types)
  • Loading branch information
keeganwitt committed Jul 25, 2023
1 parent e1e137a commit 635c178
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
7 changes: 0 additions & 7 deletions types/index.d.ts
Expand Up @@ -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<any> {}
}

// 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<any> {}
}

declare module 'jest-extended' {
const matchers: CustomMatchers<any>;
export = matchers;
Expand Down
46 changes: 42 additions & 4 deletions website/docs/getting-started/setup.md
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
```

Expand All @@ -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<R> extends Record<string, any> {
toHaveBeenCalledAfter(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoFirstInvocation: boolean,
): R;

toHaveBeenCalledBefore(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoSecondInvocation: boolean,
): R;

toHaveBeenCalledExactlyOnceWith(...args: unknown[]): R;
}

declare namespace jest {
interface Matchers<R> {
toHaveBeenCalledAfter(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoFirstInvocation?: boolean,
): R;

toHaveBeenCalledBefore(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoSecondInvocation?: boolean,
): R;
}
}

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

0 comments on commit 635c178

Please sign in to comment.