TypeScript error when passing matchers to vitest expect
diegohaz opened this issue · comments
@testing-library/jest-dom
version: 6.1.3node
version: 18.15.0jest
(orvitest
) version: vitest 0.34.4npm
(oryarn
) 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:
- Access https://stackblitz.com/edit/vitest-dev-vitest-o2n8mb?file=vitest.setup.ts&initialPath=__vitest__/
- 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 doexpect.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