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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vitest types not working (closes #610) #636

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
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
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> {}
}
```