jest-junit
A jest reporter that generates junit xml files
Last updated 2 months ago by jsonp .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jest-junit 
SYNC missed versions from official npm registry.

Build Status

jest-junit

A Jest reporter that creates compatible junit xml files

Installation

yarn add --dev jest-junit

Important Notice

Soon jest-junit will no longer function as a testResultProcessor. It will only work as a jest reporter. See the docs just below this for how to transition your project.

Usage

In your jest config add the following entry:

{
  "reporters": [ "default", "jest-junit" ]
}

Then simply run:

jest

For your Continuous Integration you can simply do:

jest --ci --reporters=default --reporters=jest-junit

Usage as testResultsProcessor

In your jest config add the following entry:

{
  "testResultsProcessor": "jest-junit"
}

Then simply run:

jest

For your Continuous Integration you can simply do:

jest --ci --testResultsProcessor="jest-junit"

Configuration

jest-junit offers several configurations based on environment variables or a jest-junit key defined in package.json or a reporter option. Environment variable and package.json configuration should be strings. Reporter options should also be strings exception for suiteNameTemplate, classNameTemplate, titleNameTemplate that can also accept a function returning a string.

Environment Variable Name Reporter Config Name Description Default Possible Injection Values
JEST_SUITE_NAME suiteName name attribute of <testsuites> "jest tests" N/A
JEST_JUNIT_OUTPUT_DIR outputDirectory Directory to save the output. process.cwd() N/A
JEST_JUNIT_OUTPUT_NAME outputName File name for the output. "junit.xml" N/A
JEST_JUNIT_SUITE_NAME suiteNameTemplate Template string for name attribute of the <testsuite>. "{title}" {title}, {filepath}, {filename}, {displayName}
JEST_JUNIT_CLASSNAME classNameTemplate Template string for the classname attribute of <testcase>. "{classname} {title}" {classname}, {title}, {filepath}, {filename}, {displayName}
JEST_JUNIT_TITLE titleTemplate Template string for the name attribute of <testcase>. "{classname} {title}" {classname}, {title}, {filepath}, {filename}, {displayName}
JEST_JUNIT_ANCESTOR_SEPARATOR ancestorSeparator Character(s) used to join the describe blocks. " " N/A
JEST_JUNIT_ADD_FILE_ATTRIBUTE addFileAttribute Add file attribute to the output. This config is primarily for Circle CI. This setting provides richer details but may break on other CI platforms. false N/A
JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT includeConsoleOutput Adds console output to any testSuite that generates stdout during a test run. false N/A
JEST_USE_PATH_FOR_SUITE_NAME usePathForSuiteName DEPRECATED. Use suiteNameTemplate instead. Use file path as the name attribute of <testsuite> "false" N/A

You can configure these options via the command line as seen below:

JEST_SUITE_NAME="Jest JUnit Unit Tests" JEST_JUNIT_OUTPUT_DIR="./artifacts" jest

Or you can also define a jest-junit key in your package.json. All are string values.

{
  ...
  "jest-junit": {
    "suiteName": "jest tests",
    "outputDirectory": ".",
    "outputName": "junit.xml",
    "classNameTemplate": "{classname}-{title}",
    "titleTemplate": "{classname}-{title}",
    "ancestorSeparator": " › ",
    "usePathForSuiteName": "true"
  }
}

Or you can define your options in your reporter configuration.

// jest.config.js
{
	reporters: [
      "default",
    	[ "jest-junit", { suiteName: "jest tests" } ]
  ]
}

Configuration Precedence

If using the usePathForSuiteName and suiteNameTemplate, the usePathForSuiteName value will take precedence. ie: if usePathForSuiteName=true and suiteNameTemplate="{filename}", the filepath will be used as the name attribute of the <testsuite> in the rendered jest-junit.xml).

Examples

Below are some example configuration values and the rendered .xml to created by jest-junit.

The following test defined in the file /__tests__/addition.test.js will be used for all examples:

describe('addition', () => {
  describe('positive numbers', () => {
    it('should add up', () => {
      expect(1 + 2).toBe(3);
    });
  });
});

Example 1

The default output:

<testsuites name="jest tests">
  <testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:42:28" time="0.161">
    <testcase classname="addition positive numbers should add up" name="addition positive numbers should add up" time="0.004">
    </testcase>
  </testsuite>
</testsuites>

Example 2

Using the classNameTemplate and titleTemplate:

JEST_JUNIT_CLASSNAME="{classname}" JEST_JUNIT_TITLE="{title}" jest

renders

<testsuites name="jest tests">
  <testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:45:42" time="0.154">
    <testcase classname="addition positive numbers" name="should add up" time="0.005">
    </testcase>
  </testsuite>
</testsuites>

Example 3

Using the ancestorSeparator:

JEST_JUNIT_ANCESTOR_SEPARATOR=" › " jest

renders

<testsuites name="jest tests">
  <testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:47:12" time="0.162">
    <testcase classname="addition › positive numbers should add up" name="addition › positive numbers should add up" time="0.004">
    </testcase>
  </testsuite>
</testsuites>

Example 4

Using the suiteNameTemplate:

JEST_JUNIT_SUITE_NAME ="{filename}" jest
<testsuites name="jest tests">
  <testsuite name="addition.test.js" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:42:28" time="0.161">
    <testcase classname="addition positive numbers should add up" name="addition positive numbers should add up" time="0.004">
    </testcase>
  </testsuite>
</testsuites>

Example 5

Using classNameTemplate as a function in reporter options

// jest.config.js
{
  reporters: [
    "default",
      [
        "jest-junit",
        {
          classNameTemplate: (vars) => {
            return vars.classname.toUpperCase();
          }
        }
      ]
  ]
}

renders

<testsuites name="jest tests">
  <testsuite name="addition" tests="1" errors="0" failures="0" skipped="0" timestamp="2017-07-13T09:42:28" time="0.161">
    <testcase classname="ADDITION POSITIVE NUMBERS" name="addition positive numbers should add up" time="0.004">
    </testcase>
  </testsuite>
</testsuites>

Current Tags

  • 8.0.0                                ...           latest (2 months ago)

36 Versions

  • 8.0.0                                ...           2 months ago
  • 7.0.0                                ...           3 months ago
  • 6.4.0                                ...           5 months ago
  • 6.3.0                                ...           8 months ago
  • 6.2.1                                ...           9 months ago
  • 6.2.0                                ...           9 months ago
  • 6.1.0                                ...           9 months ago
  • 6.0.1                                ...           9 months ago
  • 6.0.0                                ...           9 months ago
  • 5.2.0                                ...           a year ago
  • 5.1.0                                ...           a year ago
  • 5.0.0                                ...           a year ago
  • 4.0.0                                ...           a year ago
  • 3.7.0                                ...           a year ago
  • 3.6.0                                ...           2 years ago
  • 3.5.0                                ...           2 years ago
  • 3.4.1                                ...           2 years ago
  • 3.4.0                                ...           2 years ago
  • 3.3.0                                ...           2 years ago
  • 3.2.1                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 1.5.1                                ...           2 years ago
  • 1.5.0                                ...           2 years ago
  • 1.4.0                                ...           3 years ago
  • 1.3.0                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 97
This Week 248
This Month 957
Last Day 151
Last Week 543
Last Month 2,083
Dependencies (4)
Dev Dependencies (2)
Dependents (89)

Copyright 2014 - 2016 © taobao.org |