【问题标题】:Jest failing to find nested test开玩笑找不到嵌套测试
【发布时间】:2018-02-02 08:19:15
【问题描述】:

我有以下项目设置:

.
|- package.json
|- library
  |- button
    |- button.test.tsx

当我运行 jest 时,它找到了测试库/button/button.test.tsx,但给出了错误消息

ENOENT: no such file or directory, open '<root-dir>/library/button.test.tsx' 

运行测试时。

如您所见,它不是在 /library/button 中查找,而是在 /library 中查找。

我该如何解决这个问题?

package.json:

"jest": {
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "\\.(ts|tsx)$": "./node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "./library/.*.test\\.(ts|tsx|js)$",
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
      "./__mocks__/fileMock.js",
      "\\.(css|less)$": "identity-obj-proxy"
    }
  }

【问题讨论】:

标签: javascript typescript jestjs


【解决方案1】:

推荐设置roots选项:

{
  "roots": [
    "<rootDir>/library"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ]
}

我个人只使用src 文件夹。

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 2017-11-28
    • 1970-01-01
    • 2018-09-10
    • 2021-03-19
    • 2020-12-21
    • 2018-12-09
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多