Jest:“根目录[0]选项中的目录未找到”

nwwlzxa7  于 11个月前  发布在  Jest
关注(0)|答案(2)|浏览(239)

我尝试设置monorepo以一次运行所有Jest测试。在每个包中,我使用react-app-rewired让Babel转换从其他包导入的代码,而不从this article中弹出。
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.

字符串
在我的front-end文件夹(monorepo根目录)中没有src文件夹,我不明白为什么Jest试图在那里查找。
我试过在jest.config.js中提供rootDirroots,但这似乎没有什么区别。

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

  • jest.config.js(root):*
module.exports = {
  rootDir: ["."],
  projects: [
    "<rootDir>/packages/app",
    "<rootDir>/packages/components",
    "<rootDir>/packages/utils",
  ],
};

  • package.json(root):*
{
  "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"
    ]
  }
}

  • js(root):*
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)
  );
};

r7s23pms

r7s23pms1#

对我来说,下面的代码在js-overrides.js工作。

module.exports = {
  jest: function(config) {
    config.collectCoverageFrom = ['client/**/*.{js,jsx,ts,tsx}', '!client/**/*.d.ts'];
    config.testMatch = [
      '<rootDir>/client/**/__tests__/**/*.{js,jsx,ts,tsx}',
      '<rootDir>/client/**/*.{spec,test}.{js,jsx,ts,tsx}',
    ];
    config.roots = ['<rootDir>/client'];
    return config;
  },

  // The paths config
  paths: function(paths, env) {
    paths.appIndexJs = path.resolve(__dirname, 'client/index.js');
    paths.appSrc = path.resolve(__dirname, 'client');
    return paths;
  },
};

字符串
我已经将cra创建的src文件夹重命名为client,并添加了路径配置部分来反映这一点。这本身是不够的,因此jest部分。只需配置您的路径以反映您自己的结构。

fykwrbwg

fykwrbwg2#

在您的根文件夹下创建一个名为"/src"的文件夹,然后将所有__test__文件夹移动到该文件夹下。
这个文件夹结构对我很有用。


的数据

相关问题