从npm迁移至pnpm后,引入@testing-library/jest-dom/matchers类型报错求助
从npm迁移pnpm后@testing-library/jest-dom声明文件问题的解决方案
先确认
@types/testing-library__jest-dom和@testing-library/jest-dom版本匹配:
卸载现有类型包,重新安装对应版本:pnpm remove @types/testing-library__jest-dom pnpm add -D @types/testing-library__jest-dom@与jest-dom一致的版本号比如你用的
@testing-library/jest-dom是v6.4.2,就装同版本的@types包,版本不匹配会导致声明文件不生效。检查
tsconfig.json配置:
确保typeRoots包含node_modules/@types,如果有自定义类型目录也要加上:{ "compilerOptions": { "typeRoots": ["./node_modules/@types", "./src/types"] } }同时确认
include数组覆盖测试文件所在路径:{ "include": ["src/**/*", "tests/**/*"] }清除pnpm缓存重新装依赖:
缓存异常可能导致依赖加载出问题,执行:pnpm store prune pnpm install手动添加声明文件(兜底方案):
在项目里新建src/types/jest-dom.d.ts文件,写入:declare module '@testing-library/jest-dom/matchers';确保这个文件被
tsconfig.json的include包含。
内容的提问来源于stack exchange,提问作者Rozrewolwerowany rewolwer
相关产品推荐
相关产品推荐

