【问题标题】:Is there anyway to extend a jest configuration file?无论如何要扩展一个笑话配置文件吗?
【发布时间】:2020-11-10 19:33:19
【问题描述】:

在节点应用程序中,我使用 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 配置选项。使用它我必须为每个配置创建一个单独的包。这是推荐的方式吗?

【问题讨论】:

标签: jestjs


【解决方案1】:

是的,您可以定义共享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

【讨论】:

  • 我测试了你的解决方案,效果很好,但我有一个问题,想象“客户端”和“服务器”是 2 个完全不同的包,你如何处理共享的“setupFiles”和“setupFilesAfterEnv”来自您拥有可重用 jest.config.js 的同一个共享包?
  • @mejiamanuel57 您可以将其作为单独的 npm 包保存,在 2 个或 N 个独立包之间共享...
  • 看起来这只是使用 ECMA 对象传播语法。我认为这不会允许递归地继承嵌套选项。子级中的属性将完全替换父级中的相同属性,除非您也明确地将这些特定属性扩展到您想要深入的任何级别。
【解决方案2】:

是的,从 Jest v20 开始,您可以将配置定义为 JS 文件,并使用它来共享类似配置的公共部分。 Docs on configuring Jest.

默认情况下,Jest 会查找:

  • jest.config.js
  • "jest" 进入 package.json

...并将父目录视为rootDir

还请务必查看projects 选项,它可以更轻松地在 monorepos 中运行 Jest(例如,在一个代码库中运行客户端 + 服务器代码)。请参阅此答案以供参考:Testing two environments with jest

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多