jestjs - 无论如何要扩展一个 Jest 配置文件?

标签 jestjs

在我使用 Jest 测试客户端代码 (testEnvironment: 'jsdom') 和服务器端代码 (testEnvironment:'node') 以及收集客户端和服务器端的代码覆盖率的节点应用程序中。

目前我正在使用 4 个带有大量冗余配置的 Jest 配置文件来完成此操作。

客户

{
  "bail": true,
  "verbose": true,
  "notify": true,
  "scriptPreprocessor": "./node_modules/babel-jest",
  "testPathIgnorePatterns": [
    "./node_modules",
    "./coverage",
    "./dist",
    "./build"
  ],
  "testRegex": "\\.test\\.js"
}

客户覆盖
{
  "bail": true,
  "verbose": true,
  "notify": true,
  "scriptPreprocessor": "./node_modules/babel-jest",
  "testPathIgnorePatterns": [
    "./node_modules",
    "./coverage",
    "./dist",
    "./build"
  ],
  "testRegex": "\\.test\\.js",
  "collectCoverageFrom": ["**/*.js", "!**/node_modules/**"],
  "collectCoverage": true,
  "coverageDirectory": "./coverage",
  "coveragePathIgnorePatterns": [
    "./node_modules",
    "./coverage",
    "./dist",
    "./build",
    "./test"
  ],
  "coverageThreshold": {
    "global": {
      "branches": 100,
      "functions": 100,
      "lines": 100,
      "statements": 100
    }
  }
}

服务器
{
  "bail": true,
  "verbose": true,
  "notify": true,
  "scriptPreprocessor": "./node_modules/babel-jest",
  "testPathIgnorePatterns": [
    "./node_modules",
    "./coverage",
    "./dist",
    "./build"
  ],
  "testRegex": "\\.test\\.js",
  "testEnvironment": "node"
}

服务器覆盖
{
  "bail": true,
  "verbose": true,
  "notify": true,
  "scriptPreprocessor": "./node_modules/babel-jest",
  "testPathIgnorePatterns": [
    "./node_modules",
    "./coverage",
    "./dist",
    "./build"
  ],
  "testRegex": "\\.test\\.js",
  "testEnvironment": "node",
  "collectCoverageFrom": ["**/*.js", "!**/node_modules/**"],
  "collectCoverage": true,
  "coverageDirectory": "./coverage",
  "coveragePathIgnorePatterns": [
    "./node_modules",
    "./coverage",
    "./dist",
    "./build",
    "./test"
  ],
  "coverageThreshold": {
    "global": {
      "branches": 100,
      "functions": 100,
      "lines": 100,
      "statements": 100
    }
  }
}

如何在不重复配置 4 次的情况下实现这一目标?我看过 preset配置选项。使用它,我必须为每个配置创建一个单独的包。这是推荐的方式吗?

最佳答案

是的,您可以定义共享 jest.config.js并在您的特定配置中重用它:

  • 很好用<rootDir>在共享配置中的所有路径中,因此它们也可以重复使用。
  • client/jest.config.js
    const sharedConfig = require('../jest.config.js');
    module.exports = {
      ...sharedConfig,
      'rootDir': './',
    }
    
    server/jest.config.js
    const sharedConfig = require('../jest.config.js');
    module.exports = {
      ...sharedConfig,
      'rootDir': './',
      "testEnvironment": "node"
    }
    
    如果需要,您还可以重用 jest 默认值:Jest Documentation - Configuring Jest

    关于jestjs - 无论如何要扩展一个 Jest 配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40726702/

    相关文章:

    vue.js - "[vuex] state field foo was overridden by a module with the same name at foobar"与 deepmerge 辅助函数开 Jest

    angular - 使用cdkFocusInitial对 Material 对话框组件进行 Jest 单元测试时,如何解决 '[cdkFocusInitial]'不能聚焦的警告?

    javascript - import 在测试时返回 undefined 而不是 react 组件

    reactjs - Jest 与 webpack 提供插件

    testing - 试图用 Jest 测试我的嵌套 api,返回未定义

    typescript - Jest 模拟 moment()

    jestjs - 如何在VSTS测试中发布Jest单元测试结果?

    reactjs - 在 Next.js 上使用 jest 找不到模块错误

    jestjs - Jest 使用 Mock Service Worker (MSW) 或其他工具模拟离线网络?

    node.js - 用 Jest 模拟 fs 函数