4

我正在尝试设置 monorepo 以一次运行其所有 Jest 测试。在每个包中,我都使用react-app- rewired 让 Babel 转换从其他包导入的代码,而不会从 create-react-app 中弹出,如本文所述

react-app-rewired test从每个包文件夹运行时成功,但是从 monorepo 的根文件夹运行时,我收到此错误

Directory /Users/Me/go/src/gitlab.com/my-org/front-end/src in the roots[0] option was not found.

我的文件夹(monorepo 根目录)中没有src文件夹,我front-end无法弄清楚 Jest 为何试图查看那里。

我试过提供rootDirand rootsin jest.config.js,但这似乎没有什么区别。

如何让 Jest 停止查找不存在的文件夹?

jest.config.js(根):

module.exports = {
  rootDir: ["."],
  projects: [
    "<rootDir>/packages/app",
    "<rootDir>/packages/components",
    "<rootDir>/packages/utils",
  ],
};

package.json(根):

{
  "name": "root",
  "private": true,
  "workspaces": [
    "packages/*"
  ],
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
      "prettier --write"
    ]
  },
  "scripts": {
    "analyze": "source-map-explorer 'build/static/js/*.js'",
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject",
    "prettier": "prettier --check '**/*.js'",
    "prettier:fix": "prettier --write '**/*.js'"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

config-overrides.js(根):

const path = require("path");

const { override, babelInclude } = require("customize-cra");

module.exports = function (config, env) {
  return Object.assign(
    config,
    override(
      babelInclude([
        path.resolve("./packages/app"),
        path.resolve("./packages/components"),
        path.resolve("./packages/utils"),
      ])
    )(config, env)
  );
};
4

0 回答 0