【问题标题】:How do I get JUnit XML output from Jest?如何从 Jest 获取 JUnit XML 输出?
【发布时间】:2016-03-29 09:48:48
【问题描述】:

我有一个带有 Jest 测试的 React 应用程序。我正在我的package.json 中配置 Jest:

…
  "jest": {
    "setupEnvScriptFile": "./test/jestenv.js",
    "setupTestFrameworkScriptFile": "./test/setup-jasmine-env.js",
    "testRunner": "node_modules/jest-cli/src/testRunners/jasmine/jasmine2.js",
    "unmockedModulePathPatterns": [
      "./node_modules/q",
      "./node_modules/react"
    ]
  },
…

setup-jasmine-env.js 看起来像这样:

var jasmineReporters = require('jasmine-reporters');
jasmine.VERBOSE = true;
jasmine.getEnv().addReporter(
  new jasmineReporters.JUnitXmlReporter({
    consolidateAll: true,
    savePath: "output/",
    filePrefix: "test-results"
  })
);

正确设置 jasmine env 需要花费一些时间,但我在 output 目录中看不到任何内容(实际上,它不是创建的,我自己创建它也无济于事)。我怀疑我对jasmine var 的更改与 Jest 使用的不同,但我不知道如何将它们连接在一起。

【问题讨论】:

    标签: javascript jenkins jestjs


    【解决方案1】:

    如果您使用更新版本的 jest(我正在查看 16.0.2),则无需指定 testrunner,因为 jasmine 是默认值。您也不需要 jest 配置的 unmockedModulePathPatterns 部分。

    即您只需在您的package.json 中包含以下devDependencies

    "jasmine-reporters": "^2.2.0",
    "jest": "^16.0.2",
    "jest-cli": "^16.0.2"
    

    并将这个笑话配置添加到您的 package.json(注意:您不再需要 unmockedModulePathPatterns 部分):

    "jest": {
      "setupTestFrameworkScriptFile": "./setup-jasmine-env.js"
    }
    

    然后在问题中使用 Drew 的setup-jasmine-env.js

    【讨论】:

    • 这对我有用;谢谢。但是,我确实不得不使用"setupTestFrameworkScriptFile": "./setup-jasmine-env.js" 开玩笑来定位模块。
    【解决方案2】:

    Jest 通过 testResultsProcessor 配置支持自己的报告器。所以我写了一个小东西来为此生成兼容的junit xml。你可以在这里找到它。 https://github.com/palmerj3/jest-junit

    【讨论】:

    【解决方案3】:

    您在上述设置中缺少的只是将jasmine-reporters 添加到unmockedModulePathPatterns,因此请尝试以下操作:

    "jest": {
    
       ...
    
       "unmockedModulePathPatterns": [
         "./node_modules/q",
         "./node_modules/react",
         "./node_modules/jasmine-reporters"
       ]
    },
    

    希望对您有所帮助!

    更新:对于遇到此问题的其他人,我已经在 here 上发布了一个工作演示。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2021-07-28
    • 2020-02-25
    • 1970-01-01
    相关资源
    最近更新 更多