16.0.0 • Published 1 year ago

jest-junit v16.0.0

Weekly downloads
1,542,644
License
Apache-2.0
Repository
github
Last release
1 year ago

Actions Status

jest-junit

A Jest reporter that creates compatible junit xml files

Note: as of jest-junit 11.0.0 NodeJS >= 10.12.0 is required.

Installation

yarn add --dev jest-junit

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 (deprecated)

The support for testResultsProcessor is only kept for legacy reasons and might be removed in the future. You should therefore prefer to configure jest-junit as a reporter.

Should you still want to, add the following entry to your jest config:

{
  "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 NameReporter Config NameDescriptionDefaultPossible Injection Values
JEST_SUITE_NAMEsuiteNamename attribute of <testsuites>"jest tests"N/A
JEST_JUNIT_OUTPUT_DIRoutputDirectoryDirectory to save the output. Relative path outside of project root (e.g. in monorepos) has to be prefixed with <rootDir> literal, e.g. <rootDir>/../coverageprocess.cwd()N/A
JEST_JUNIT_OUTPUT_NAMEoutputNameFile name for the output."junit.xml"N/A
JEST_JUNIT_OUTPUT_FILEoutputFileFullpath for the output. If defined, outputDirectory and outputName will be overriddenundefinedN/A
JEST_JUNIT_UNIQUE_OUTPUT_NAMEuniqueOutputNameCreate unique file name for the output leveraging the outputName as a prefix if given ${outputName}-${uuid}.xml or a default of junit-${uuid}.xml if outputName is not specified, overrides outputNamefalseN/A
JEST_JUNIT_SUITE_NAMEsuiteNameTemplateTemplate string for name attribute of the <testsuite>."{title}"{title}, {filepath}, {filename}, {displayName}
JEST_JUNIT_CLASSNAMEclassNameTemplateTemplate string for the classname attribute of <testcase>."{classname} {title}"{classname}, {title}, {suitename}, {filepath}, {filename}, {displayName}
JEST_JUNIT_TITLEtitleTemplateTemplate string for the name attribute of <testcase>."{classname} {title}"{classname}, {title}, {filepath}, {filename}, {displayName}
JEST_JUNIT_ANCESTOR_SEPARATORancestorSeparatorCharacter(s) used to join the describe blocks." "N/A
JEST_JUNIT_ADD_FILE_ATTRIBUTEaddFileAttributeAdd file attribute to the output (validated on CIRCLE CI and GitLab CI). Must be a string."false"N/A
JEST_JUNIT_FILE_PATH_PREFIXfilePathPrefixPrefix to add to the test suite file path. The value will be prefixed using path.join. Useful in case of monorepo""N/A
JEST_JUNIT_INCLUDE_CONSOLE_OUTPUTincludeConsoleOutputAdds console output to any testSuite that generates stdout during a test run.falseN/A
JEST_JUNIT_INCLUDE_SHORT_CONSOLE_OUTPUTincludeShortConsoleOutputAdds short console output (only message value) to any testSuite that generates stdout during a test run.falseN/A
JEST_JUNIT_REPORT_TEST_SUITE_ERRORSreportTestSuiteErrorsReports test suites that failed to execute altogether as error. Note: since the suite name cannot be determined from files that fail to load, it will default to file path.falseN/A
JEST_JUNIT_NO_STACK_TRACEnoStackTraceOmit stack traces from test failure reports, similar to jest --noStackTracefalseN/A
JEST_USE_PATH_FOR_SUITE_NAMEusePathForSuiteNameDEPRECATED. Use suiteNameTemplate instead. Use file path as the name attribute of <testsuite>"false"N/A
JEST_JUNIT_TEST_CASE_PROPERTIES_JSON_FILEtestCasePropertiesFileName of the custom testcase properties file"junitProperties.js"N/A
JEST_JUNIT_TEST_CASE_PROPERTIES_DIRtestCasePropertiesDirectoryLocation of the custom testcase properties fileprocess.cwd()N/A
JEST_JUNIT_TEST_SUITE_PROPERTIES_JSON_FILEtestSuitePropertiesFileName of the custom testsuite properties file"junitTestCaseProperties.js"N/A
JEST_JUNIT_TEST_SUITE_PROPERTIES_DIRtestSuitePropertiesDirectoryLocation of the custom testsuite properties fileprocess.cwd()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",
    "uniqueOutputName": "false",
    "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>

Adding custom testsuite properties

New feature as of jest-junit 11.0.0!

Create a file in your project root directory named junitProperties.js:

module.exports = () => {
  return {
    key: "value",
  };
};

Will render

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

Adding custom testcase properties

Create a file in your project root directory named junitTestCaseProperties.js:

module.exports = (testResult) => {
  return {
    "dd_tags[test.invocations]": testResult.invocations,
  };
};

Will render

<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">
      <properties>
        <property name="dd_tags[test.invocations]" value="1" />
      </properties>
    </testcase>
  </testsuite>
</testsuites>

WARNING: Properties for testcases is not following standard JUnit XML schema. However, other consumers may support properties for testcases like DataDog metadata through <property> elements

@storybook/test-runner@knapsack-cloud/msk-design-system@fairfx/spend-commonshared-express-servergithub-actions-playground-core-3github-actions-playground-core-4harrisonleach1-my-app@jjtang/github-actions-playground-core-5sales-insights-uici-common-ui@njmaeff/sdk-babel-typescripttruist-portal-library@bolder/jest-helpers@gooddata/sdk-ui-tests@minta/minta-testspermissioning-smart-contractsmyfrontdesk-test@payfit/todd-cli@lukeshay/jest-configthibaultpackagesp-gui-electrontitles-workflow-server@personio/configs-base-jesttest-react-scripts-cond@everything-registry/sub-chunk-1956@apolitical/testing@angular-ru/jest@aws/universal-test-adapter-jest@oqton/jest-config-oqton@albandamet/encore-un-test@albandamet/test-scope-alban@autorest/gotest@aws-cdk-testing/cli-integbaldrick-dev-tsbifrost-benchballyhoo@botonic/dx@brickjs/devops@crediwatch/web-components@coderich/ratchet@cogep/design-systembrickjs-devops@consensys-software/permissioning-smart-contracts@codeplant-de/jest-config@cubabit/es-policydex-reactnpo-platform-uinpm-hello-world-exemple-bligunomni-common-uiopenfin-service-toolingcontribute-ui@digest/jest-junit@design-systems/test@billogram/task-runner@billogram/task-ts-test@billogram/jest-config-frontend@billogram/jest-config-nodenolanpro-modelernorm-build-jestdelectusconsequunturprobundlepackage-test-albanpartner-center-brokerpl-claims-hubdoloremad@corex/jest@blockmatic/dev-scriptsbootcamp-node-project-arshiaabootcamp-node-project-arshiaaabootcamp-node-project-arshiaaaa@davidwl/pluggable-widgets-tools@daanv2/hueroc-plugin-test-jesteslint-config-cisync-linter@graphqlbase/react-scriptsrn-detox-e2e-test-helper@eventbrite/brite-core@evolab/testutils@highlight-ui/configs-base-jest@hinng/remove_duplicates_array@html-validate/jest-configsanity-runner-service@dragongate/react-scripts@drill4j/ui-kitcomponent-adapter-interfacescoteh-react-scripts@infura/sdk@interaktiv/dia-scriptsfareeda-component-libraryfareeda-libraryservicebus-user-login-trigger@eaglegis/weather-widget@forsakringskassan/jest-configreactive-event-busrewardsebs-form@grafarg/toolkitorchestrate-ui-coreorc-scriptsratchadj-integration-test
16.0.0

1 year ago

15.0.0

1 year ago

14.0.1

2 years ago

14.0.0

2 years ago

13.2.0

2 years ago

13.1.0

2 years ago

13.0.0

3 years ago

12.3.0

3 years ago

12.1.0

3 years ago

12.2.0

3 years ago

12.0.0

4 years ago

11.1.0

4 years ago

11.0.1

4 years ago

11.0.0

4 years ago

10.0.0

4 years ago

9.0.0

4 years ago

8.0.0

5 years ago

7.0.0

5 years ago

6.4.0

5 years ago

6.3.0

5 years ago

6.2.1

5 years ago

6.2.0

5 years ago

6.1.0

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.2.0

6 years ago

5.1.0

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.7.0

6 years ago

3.6.0

6 years ago

3.5.0

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.3.0

6 years ago

3.2.1

6 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago