jest-dom: TS error when using vitest globals and @testing-library/jest-dom
@testing-library/jest-dom
version: 5.16.1node
version: 14.17.0yarn
version: 1.22.17@testing-library/react
version: 12.1.2
I am using vitest
instead of jest
for testing a React component lib in a monorepo. I am using @testing-library/jest-dom
and @testing-library/react
and both happily work with vitest
.
However, when type checking my code I am seeing a clash between @types/jest
and vitest
. The @types/jest
dependency seems to have been pulled in by @testing-library/jest-dom
.
../node_modules/@types/jest/index.d.ts:34:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: test, describe, it, expect, beforeAll, afterAll, beforeEach, afterEach
34 declare var beforeAll: jest.Lifecycle;
~~~~~~~
../node_modules/vitest/global.d.ts:1:1
1 declare global {
~~~~~~~
Conflicts are in this file.
../node_modules/vitest/global.d.ts:1:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: test, describe, it, expect, beforeAll, afterAll, beforeEach, afterEach
1 declare global {
~~~~~~~
../node_modules/@types/jest/index.d.ts:34:1
34 declare var beforeAll: jest.Lifecycle;
~~~~~~~
Conflicts are in this file.
Found 2 errors.
It would be great to have a workaround for this issue so the jest
types can be removed or ignored.
To reproduce this issue clone the following project (notice it uses a branch called yarn
):
https://github.com/robcaldecott/pnpm-vite-monorepo-example/tree/yarn
To see the error:
yarn install
cd components
yarn build
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 48
- Comments: 25 (2 by maintainers)
Links to this issue
Commits related to this issue
- examples(full-stack): fix vitest and testing-library types See https://github.com/testing-library/jest-dom/issues/427#issuecomment-1420845247 for the fix. This is also a possible fix for https://gith... — committed to swarmion/swarmion by adriencaccia a year ago
- examples(full-stack): fix vitest and testing-library types See https://github.com/testing-library/jest-dom/issues/427#issuecomment-1420845247 for the fix. This is also a possible fix for https://gith... — committed to swarmion/swarmion by adriencaccia a year ago
- Fix issues with vitest and types See https://github.com/testing-library/jest-dom/issues/427 — committed to johnhunter/mondo by johnhunter 9 months ago
Hi. How about providing
@testing-library/vitest-dom
? Since the release of Vitest, the number of downloads of Vitest has increased. I think the official support for using Testing Library with Vitest makes us very happy and we don’t have to add ad-hoc solutions.@gnapse is there any possibility to remove
/// <reference types="jest" />
from library types and make@types/jest
a peerDependency?Anyway, I found a slightly less hacky workaround 💯 :
setupTests.ts
(file which goes totest.setupFiles
invite.config.ts
)global.d.ts
(file placed in root or insidesrc
directory)tsconfig.json
Tried the workaround above, but had to tweak one thing. In
global.d.ts
I had to write this instead:I like this approach as well.
For now, I am using a workaround with
pnpm patch
, creating this diff to empty the definitions in@types/testing-library__jest-dom/index.d.ts
:In
patches/@types__testing-library__jest-dom@5.14.5.patch
, with this inpackage.json
:Paired with this in my Vitest setup file: https://github.com/testing-library/jest-dom/issues/439#issuecomment-1087504347
I made a simple solution to fix this issue: https://github.com/zoontek/types-testing-library-vitest-dom
Installation (yarn)
Installation (npm)
Test setup file
🎉 This issue has been resolved in version 6.0.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
Solution for
pnpm
users:Add this to your
package.json
:Now, create a file
patches/@types__testing-library__jest-dom@5.14.5.patch
with the following contents:Save both and run
pnpm i
.Not sure if this is the correct solution but adding
"skipLibCheck": true,
in mytsconfig.json
fixed it.Another solution if you don’t mind to install a new package is to install vitest-dom. no hack or workaround and it works straightforward with pnpm too. It is not part of testing-library though.
Do you have any updates on the status of this issue? For now, the approach I’ve followed is the one mentioned into another issue (which targets the latest version of Vite)
I don’t think making a new package is worth the cognitive/maintenance overhead when we could just make this package more framework agnostic for now.
However, I vaguely remember another maintainer planning on refactoring matchers out of this package. Please let me know if there’s an update on that. I couldn’t find the source, but I don’t want to step on any toes accidentally.
for me, using
vitest@0.16.0
and@testing-library/jest-dom@5.16.4
only the solution bellow workedand using
pnpm
the code bellow need to be placed in the.npmrc
to be able to use theTestingLibraryMatchers
types outsidenode_modules
Would the workaround here work for this? https://github.com/testing-library/jest-dom/issues/439
Combining @BPreisner and @airjp73’s solutions, I made a new template to save myself (and others) future time: https://github.com/jsjoeio/react-ts-vitest-template
FWIW this works just fine too. (I tend not to care about throwing errors because they are of limited value in a build step like this)
EDIT: my vite.config.js and tsconfig.json for reference. @laruiss ☝️