最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I get JUnit XML output from Jest? - Stack Overflow

programmeradmin0浏览0评论

I have a React app that has Jest tests. I'm configuring Jest in my package.json:

…
  "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"
    ]
  },
…

The setup-jasmine-env.js looks like this:

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

It took a bit of working to get that jasmine env setup correctly, but I"m not seeing anything in the output directory (indeed, it isn't created and creating it myself doesn't help). I suspect that my alterations to the jasmine var aren't the same one that Jest is using, but I can't figure out how to hook them together.

I have a React app that has Jest tests. I'm configuring Jest in my package.json:

…
  "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"
    ]
  },
…

The setup-jasmine-env.js looks like this:

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

It took a bit of working to get that jasmine env setup correctly, but I"m not seeing anything in the output directory (indeed, it isn't created and creating it myself doesn't help). I suspect that my alterations to the jasmine var aren't the same one that Jest is using, but I can't figure out how to hook them together.

Share Improve this question asked Dec 23, 2015 at 2:12 Drew StephensDrew Stephens 17.8k17 gold badges68 silver badges82 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

If you use a more recent version of jest (I'm looking at 16.0.2), you don't need to specify the testrunner because jasmine is the default. You also don't need the unmockedModulePathPatterns section of the jest config.

I.e. you just need to include the following devDependencies in your package.json:

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

And add this jest config to your package.json (note: you no longer need the unmockedModulePathPatterns section):

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

And then use Drew's setup-jasmine-env.js from the question.

Jest has support for its own reporters via the testResultsProcessor config. So I wrote up a little thing that generates compatible junit xml for this. You can find it here. https://github.com/palmerj3/jest-junit

looks like all you're missing from the above setup is to add jasmine-reporters to unmockedModulePathPatterns, so give the following a go:

"jest": {

   ...

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

Hope that helps!

UPDATE: for anyone else experiencing this problem, I have put a working demo up here.

发布评论

评论列表(0)

  1. 暂无评论