testing-library / jest-dom

:owl: Custom jest matchers to test the state of the DOM

Home Page:https://testing-library.com/docs/ecosystem-jest-dom

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript error when passing matchers to vitest expect

diegohaz opened this issue · comments

commented
  • @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

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

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