首页 > 解决方案 > jsconfig 和 jest 不交互以使用相同的根路径

问题描述

我正在构建一个 React 项目,目前在进行测试时遇到问题使用 jsconfig 来使用根路径。

结构是这样的,

├── divjoy-project.json
├── jsconfig.json
├── next.config.js
├── node_modules
├── package.json
├── package-lock.json
├── postcss.config.js
├── public
├── README.md
├── src
├── tailwind.config.js
├── worker
└── yarn.lock

jsconfig 很简单,

{
  "compilerOptions": {
    "baseUrl": "./src"
  }
}

开玩笑的是

 "jest": {
    "testEnvironment": "node",
    "testMatch": [
      "**/?(*.)(spec|test).?(m)js?(x)"
    ],
    "rootDir":"./src",
    "moduleFileExtensions": [
      "js",
      "jsx",
      "mjs"
    ],
    "transform": {
      "^.+\\.m?jsx?$": "babel-jest"
    },
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": -10
      }
    }
  },

我也在使用 next.js。谢谢!

标签: javascriptreactjstestingjestjs

解决方案


尝试添加"moduleDirectories": ['node_modules', '<rootDir>']您的笑话配置。这就是开玩笑找到你的 jsconfig 文件。

就我而言,我有一个 jest.config.js 文件:

module.exports = {
  moduleDirectories: ['node_modules', '<rootDir>'],
  // other configs
}

我也在使用 next.js,上面的内容对我有用。


推荐阅读