覆盖 jest-junit 默认输出位置?

mat*_*sev 5 javascript xunit jestjs jest-junit

如何覆盖默认输出目录和文件名jest-junit

我想在使用Jest时自定义输出的输出配置,但它仍然在默认位置,即./junit.xml在项目根文件夹中。


我的配置

package.json 中

  "scripts": {
    "test": "jest --ci --reporters=default --reporters=jest-junit"
  },
  "devDependencies": {
    "jest": "^24.9.0",
    "jest-junit": "^10.0.0",
  }
Run Code Online (Sandbox Code Playgroud)

jest.config.js 中

module.exports = {
  testMatch: [
    '**/*.spec.js',
  ],
  reporters: [
    'default',
    [ 'jest-junit', {
      outputDirectory: 'test_reports',
      outputName: 'jest-junit.xml',
    } ]
  ]
};
Run Code Online (Sandbox Code Playgroud)

预期结果:

  • 包含测试结果的文件 ./test_reports/jest-junit.xml

实际结果:

  • 默认为测试结果的文件 ./junit.xml

mat*_*sev 14

解决方案

更改package.json脚本自

"scripts": {
  "test": "jest --ci --reporters=default --reporters=jest-junit"
},
Run Code Online (Sandbox Code Playgroud)

只是

"scripts": {
  "test": "jest --ci"
},
Run Code Online (Sandbox Code Playgroud)

解释

问题是记者配置已提供给package.json 中的jest.config.js文件以及test脚本的 cli 参数。CLI选项具有更高的优先级,并且不提供“outputDirectory和” outputName,测试结果将恢复为默认值junit.xml