Skip to content

Commit

Permalink
fix: vitest types not working (closes #610) (#636)
Browse files Browse the repository at this point in the history
* fix: vitest types not working (closes #610)
(rely on user provided types)

* Update website/docs/getting-started/setup.md

Co-authored-by: Simen Bekkhus <[email protected]>

---------

Co-authored-by: Simen Bekkhus <[email protected]>
  • Loading branch information
keeganwitt and SimenB authored Aug 2, 2023
1 parent e327bce commit 1609897
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-buckets-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jest-extended': patch
---

Remove problematic Vitest types
12 changes: 0 additions & 12 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
47 changes: 43 additions & 4 deletions website/docs/getting-started/setup.md
Original file line number Diff line number Diff line change
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,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
/// <reference types="vitest" />

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;
}
```

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<T = any> extends CustomMatchers<T> {}
}
```

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

0 comments on commit 1609897

Please sign in to comment.