-
Notifications
You must be signed in to change notification settings - Fork 404
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
TS error when using vitest globals and @testing-library/jest-dom #427
Comments
Simply removing the following line from the types definition solves it and doesn't impact the functionality from my brief testing.
If anyone wants a hacky and crude solution in the meantime: package.json
patchJestDom.js
|
@dancras thanks for the hack, currently using it too but added to
|
Thanks @dancras, your solution worked! I had to modify it because my reference to jest was on line 10, so I ended up making it to recognize any line: const fs = require("fs");
const path = require("path");
const typesPath = path.resolve(
"node_modules",
"@types",
"testing-library__jest-dom",
"index.d.ts"
);
fs.readFile(typesPath, "utf8", (err, data) => {
if (err) throw err;
let lines = data.split("\n");
jestTypesIndex = lines.findIndex((line) =>
line.includes('reference types="jest"')
);
if (lines[jestTypesIndex] === '/// <reference types="jest" />') {
lines = lines
.slice(0, jestTypesIndex)
.concat(lines.slice(jestTypesIndex + 1));
}
fs.writeFile(typesPath, lines.join("\n"), "utf8", function (err) {
if (err) throw err;
});
}); |
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) import fs from 'fs'
import path from 'path'
const typesFile = path.resolve('node_modules/@types/testing-library__jest-dom/index.d.ts')
const encoding = 'utf8'
const strings = {
search: '/// <reference types="jest" />',
replace: '// See https://github.com/testing-library/jest-dom/issues/427 for reference',
}
const result = fs
.readFileSync(typesFile, encoding)
.replace(strings.search, strings.replace)
fs.writeFileSync(typesFile, result, encoding) EDIT: my vite.config.js and tsconfig.json for reference. |
@larsenwork Would you be so kind as to share your |
@gnapse is there any possibility to remove Anyway, I found a slightly less hacky workaround 💯 :
import '@testing-library/jest-dom/extend-expect';
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';
declare global {
namespace jest {
type Matchers<R = void, T = {}> = TestingLibraryMatchers<
typeof expect.stringContaining,
R
>;
}
}
{
"compilerOptions": {
...
"types": ["node", "vite/client", "vitest/globals"],
},
}
|
Tried the workaround above, but had to tweak one thing. In import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
declare global {
namespace jest {
interface Matchers<R = void>
extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}
} |
Not sure if this is the correct solution but adding |
for me, using // global.d.ts
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';
type CustomMatchers<R = unknown> = TestingLibraryMatchers<typeof expect.stringContaining, R>;
declare global {
namespace Vi {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
} and using shamefully-hoist = true |
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 |
@BPreisner "compilerOptions": {
"typeRoots": [],
"types": [],
} I know it doesn't fix the root problem of them bundling the types. |
I made a simple solution to fix this issue: https://github.com/zoontek/types-testing-library-vitest-dom Installation (yarn)"resolutions": {
"@types/testing-library__jest-dom": "github:zoontek/types-testing-library-vitest-dom"
} Installation (npm)"overrides": {
"@types/testing-library__jest-dom": "github:zoontek/types-testing-library-vitest-dom"
} Test setup fileimport matchers from "@testing-library/jest-dom/matchers";
import { expect } from "vitest";
expect.extend(matchers); |
Hi. |
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. |
Would the workaround here work for this? #439 |
Are you suggesting we document this as a temporary workaround for Vitest users, or expose a Vitest entry point permanently? |
Currently a workaround, I can see why the requirement of |
I like this approach as well. For now, I am using a workaround with diff --git a/index.d.ts b/index.d.ts
index 43ba6b7fe458e77d152fe0b2f7afeac05d8fc563..dfa540aa7d1ab8c78c31104d3210eee3c33a94c0 100755
--- a/index.d.ts
+++ b/index.d.ts
@@ -7,12 +7,9 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 4.3
-/// <reference types="jest" />
+// NOTE: these definitions are left blank, to support using Testing Library Jest matchers with
+// Vitest, without having to use Jest's global types (for `expect`, etc).
-import { TestingLibraryMatchers } from './matchers';
+// Vitest's `expect` type is instead extended in `src/setupTests.ts`.
-declare global {
- namespace jest {
- interface Matchers<R = void, T = {}> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
- }
-}
+// For more context, see https://github.com/testing-library/jest-dom/issues/427 In
Paired with this in my Vitest setup file: #439 (comment) import matchers, {
TestingLibraryMatchers,
} from '@testing-library/jest-dom/matchers';
declare global {
namespace Vi {
interface JestAssertion<T = any>
extends jest.Matchers<void, T>,
TestingLibraryMatchers<T, void> {}
}
}
expect.extend(matchers); |
The workaround (hack 😆) that worked for me was to copy globals.d.ts from vitest to my global.d.ts file
And adding the following to compilerOptions in my tsconfig:
|
Solution for Add this to your "pnpm": {
"patchedDependencies": {
"@types/testing-library__jest-dom@5.14.5": "patches/@types__testing-library__jest-dom@5.14.5.patch"
}
} Now, create a file diff --git a/index.d.ts b/index.d.ts
index 43ba6b7fe458e77d152fe0b2f7afeac05d8fc563..3bf91587abf21f15ac166bcae1f5a59b23884b87 100755
--- a/index.d.ts
+++ b/index.d.ts
@@ -7,7 +7,7 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 4.3
-/// <reference types="jest" />
+// See https://github.com/testing-library/jest-dom/issues/427 for reference
import { TestingLibraryMatchers } from './matchers'; Save both and run |
See testing-library/jest-dom#427 (comment) for the fix. This is also a possible fix for vitest-dev/vitest#3156.
See testing-library/jest-dom#427 (comment) for the fix. This is also a possible fix for vitest-dev/vitest#3156.
Do you have any updates on the status of this issue? |
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. |
I'm also seeing this issue on a brand new project |
🎉 This issue has been resolved in version 6.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@testing-library/jest-dom
version: 5.16.1node
version: 14.17.0yarn
version: 1.22.17@testing-library/react
version: 12.1.2I am using
vitest
instead ofjest
for testing a React component lib in a monorepo. I am using@testing-library/jest-dom
and@testing-library/react
and both happily work withvitest
.However, when type checking my code I am seeing a clash between
@types/jest
andvitest
. The@types/jest
dependency seems to have been pulled in by@testing-library/jest-dom
.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:
The text was updated successfully, but these errors were encountered: