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

TypeScript error when passing matchers to vitest expect #537

Open
diegohaz opened this issue Sep 20, 2023 · 4 comments
Open

TypeScript error when passing matchers to vitest expect #537

diegohaz opened this issue Sep 20, 2023 · 4 comments

Comments

@diegohaz
Copy link
Contributor

  • @testing-library/jest-dom version: 6.1.3
  • node version: 18.15.0
  • jest (or vitest) version: vitest 0.34.4
  • npm (or yarn) version: npm 9.5.0

Relevant code or config:

import '@testing-library/jest-dom/vitest';
import * as matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';

expect.extend(matchers);

What you did:

Tried to setup @testing-library/jest-dom v6 with vitest.

What happened:

Got a TypeScript error:

Argument of type '{ default: TestingLibraryMatchers<any, void> & Record<string, any>; toBeInTheDOM(container?: HTMLElement | SVGElement | undefined): void; ... 25 more ...; toHaveErrorMessage(text?: any): void; }' is not assignable to parameter of type 'MatchersObject<MatcherState>'.
  Index signature for type 'string' is missing in type '{ default: TestingLibraryMatchers<any, void> & Record<string, any>; toBeInTheDOM(container?: HTMLElement | SVGElement | undefined): void; ... 25 more ...; toHaveErrorMessage(text?: any): void; }'.

Reproduction:

  1. Access https://stackblitz.com/edit/vitest-dev-vitest-o2n8mb?file=vitest.setup.ts&initialPath=__vitest__/
  2. See the TS error on the vitest.setup.ts file
@jgoz
Copy link
Collaborator

jgoz commented Sep 20, 2023

If you are using import '@testing-library/jest-dom/vitest', you don't need to do expect.extend(matchers). Just one or the other.

i.e., just this:

import '@testing-library/jest-dom/vitest';

Or just this:

import * as matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';

expect.extend(matchers);

But in the second version you will also need to augment the global matcher types for vitest

@bradleyball
Copy link

If you are using import '@testing-library/jest-dom/vitest', you don't need to do expect.extend(matchers). Just one or the other.

i.e., just this:

import '@testing-library/jest-dom/vitest';

Or just this:

import * as matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';

expect.extend(matchers);

But in the second version you will also need to augment the global matcher types for vitest

I'm not able to get either one to work with version 6 or greater. I'm still getting the following error:

Error: Missing "./extend-expect" specifier in "@testing-library/jest-dom" package ❯ e node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:21445:25 ❯ n node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:21445:627 ❯ o node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:21445:1297 ❯ resolveExportsOrImports node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:28741:20 ❯ resolveDeepImport node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:28760:31 ❯ tryNodeResolve node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:28448:20 ❯ Context.resolveId node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:28207:28 ❯ Object.resolveId node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:44269:32 ❯ TransformContext.resolve node_modules/vitest/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:43985:23

@mibcadet
Copy link
Contributor

If you are using import '@testing-library/jest-dom/vitest', you don't need to do expect.extend(matchers). Just one or the other.

i.e., just this:

import '@testing-library/jest-dom/vitest';

Or just this:

import * as matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';

expect.extend(matchers);

But in the second version you will also need to augment the global matcher types for vitest

I tried that, but in both versions typescript dont recognize matchers. Actually they work while running tests but still in IDE typescript shouts those matchesr are not recognized. I also added in tsconfig "vitest/globals" but I guess something more related to jsdom would be needed.

@amirhosseinNouri
Copy link

Any workaround for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants