如何配置TS来查找@testing-library/jest-dom的类型

amrnrhlw  于 11个月前  发布在  Jest
关注(0)|答案(1)|浏览(187)

这是我的tsconfig文件

{
  "compilerOptions": {
    "noEmit": true,
    "allowJs": true,
    "esModuleInterop": true,
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "baseUrl": "./",
    "skipLibCheck": false,
    "rootDirs": [
      "./src",
      "../.redwood/types/mirror/web/src",
      "../api/src",
      "../.redwood/types/mirror/api/src"
    ],
    "paths": {
      "src/*": [
        "./src/*",
        "../.redwood/types/mirror/web/src/*",
        "../api/src/*",
        "../.redwood/types/mirror/api/src/*"
      ],
      "$api/*": [ "../api/*" ],
      "types/*": ["./types/*", "../types/*"],
      "@redwoodjs/testing": ["../node_modules/@redwoodjs/testing/web"]
    },
    "typeRoots": ["../node_modules/@types", "./node_modules/@types"],
    "types": ["jest", "@testing-library/jest-dom"],
    "jsx": "preserve"
  },
  "include": [
    "src",
    "../.redwood/types/includes/all-*",
    "../.redwood/types/includes/web-*",
    "../types",
    "./types"
  ]
}

字符串
当我运行tsc --noEmit --skipLibCheck时,我得到这个错误

error TS2688: Cannot find type definition file for '@testing-library/jest-dom'.
  The file is in the program because:
    Entry point of type library '@testing-library/jest-dom' specified in compilerOptions

  tsconfig.json:29:23
    29     "types": ["jest", "@testing-library/jest-dom"],
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    File is entry point of type library specified here.

Found 1 error.


这是我的package.json

{
  "name": "web",
  "version": "0.0.0",
  "private": true,
  "browserslist": {
    "development": [
      "last 1 version"
    ],
    "production": [
      "defaults"
    ]
  },
  "dependencies": {
    "@redwoodjs/auth-dbauth-web": "7.0.0-canary.711",
    "@redwoodjs/forms": "7.0.0-canary.711",
    "@redwoodjs/router": "7.0.0-canary.711",
    "@redwoodjs/web": "7.0.0-canary.711",
    "humanize-string": "2.1.0",
    "react": "0.0.0-experimental-e5205658f-20230913",
    "react-dom": "0.0.0-experimental-e5205658f-20230913"
  },
  "devDependencies": {
    "@redwoodjs/vite": "7.0.0-canary.711",
    "@testing-library/jest-dom": "^6.1.5",
    "@types/react": "18.2.37",
    "@types/react-dom": "18.2.15",
    "autoprefixer": "^10.4.16",
    "postcss": "^8.4.32",
    "postcss-loader": "^7.3.3",
    "prettier-plugin-tailwindcss": "0.4.1",
    "tailwindcss": "^3.3.6"
  }
}


我需要做什么来让TS找到@testing-library/jest-dom的类型?
更多详情:
这在@testing-library/jest-dom v5中是有效的,但在我们升级到v6时就失效了
如果我在tsconfig中从types中删除它,我会得到关于缺少属性的类型错误(参见注解)。我可以通过在我的一个测试文件中包含import '@testing-library/jest-dom'来解决这个问题。但是我以前不必这样做,我想知道为什么现在必须这样做。

qv7cva1a

qv7cva1a1#

解决方案是将我的tslog.json从以下位置更改为:

"typeRoots": ["../node_modules/@types", "./node_modules/@types"],
    "types": ["jest", "@testing-library/jest-dom"],

字符串
对此:

"typeRoots": ["../node_modules/@types", "./node_modules/@types", "../node_modules/@testing-library"],
    "types": ["jest", "jest-dom"],


@testing-library/jest-dom的类型过去是由https://www.npmjs.com/package/@types/testing-library__jest-dom提供的,但现在它们是随主包本身提供的。因此,只在node_modules/@types中查找已经不够了。我现在需要告诉TS也在../node_modules/@testing-library中查找类型,所以我将其添加到typeRoots中。在该路径中,它应该查找类型“jest-dom”,如types中所指定。

相关问题