1.0.1 • Published 8 months ago

junit-xml-mocha-reporter v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

junit-xml-mocha-reporter

NPM Version GitHub Release GitHub License

This mocha reporter produces JUnit-style XML test results and allows you to split the same test into different test cases by jira id.

Table of Contents

Installation

 pnpm install junit-xml-mocha-reporter --save-dev
 npm install junit-xml-mocha-reporter --save-dev

Usage

Run mocha with junit-xml-mocha-reporter:

 mocha test --reporter junit-xml-mocha-reporter

This will output a results file at ./results.xml. You may optionally declare an alternate location for results XML file by setting the environment variable MOCHA_FILE or specifying mochaFile in reporterOptions:

 MOCHA_FILE=./path_to_your/file.xml mocha test --reporter junit-xml-mocha-reporter

or

 mocha test --reporter junit-xml-mocha-reporter --reporter-options mochaFile=./path_to_your/file.xml

or

const mocha = new Mocha({
    reporter: 'junit-xml-mocha-reporter',
    reporterOptions: {
        mochaFile: './path_to_your/file-results.xml'
    }
});

Split the same test case by id of jira

You can also add the jira key in the reporterOptions or in environment variables to split the same test into different test cases. This is useful if you want to test different tests in the same test in Cypress without affecting performance. If you want provided more than one jira id you should divide the ids with the , separator. e.g: title one JIRA.KEY.1,JIRA.KEY.2,JIRA.KEY.3

!IMPORTANT You shoult put the jira ids in the end of the title. e.g: title one JIRA.KEY.1,JIRA.KEY.2,JIRA.KEY.3.

To do this, enter them using the environment variable:

PROPERTIES=JIRA.KEY mocha test --reporter junit-xml-mocha-reporter

or

const mocha = new Mocha({
    reporter: 'junit-xml-mocha-reporter',
    reporterOptions: {
        jiraId: 'JIRA.KEY'
    }
})

If the test case it's this:

describe("test", () => {
  it("testcase JIRA.KEY.1,JIRA.KEY.2,JIRA.KEY.3", () => {
      expect(2).to.be.greaterThan(1);
    }
  );
});

the result it's this:

<testsuites>
  <testsuite>
    <testcase name="test testcase JIRA.KEY.1" time="0.022" classname="test test JIRA.KEY.1">
    </testcase>
    <testcase name="test testcase JIRA.KEY.2" time="0.022" classname="test test JIRA.KEY.2">
    </testcase>
    <testcase name="test testcase JIRA.KEY.3" time="0.022" classname="test test JIRA.KEY.3">
    </testcase>
  </testsuite>
</testsuites>

Append properties to testsuite

You can also add properties to the report under testsuite. This is useful if you want your CI environment to add extra build props to the report for analytics purposes

<testsuites>
  <testsuite>
    <properties>
      <property name="PROPERTY_ID" value="12345"/>
    </properties>
    <testcase/>
  </testsuite>
</testsuites>

To do so pass them in via env variable:

PROPERTIES=PROPERTY_ID:12345 mocha test --reporter junit-xml-mocha-reporter

or

const mocha = new Mocha({
    reporter: 'junit-xml-mocha-reporter',
    reporterOptions: {
        properties: {
            PROPERTY_ID: 12345
        }
    }
})

Results Report

Results XML filename can contain [hash], e.g. ./path_to_your/results.[hash].xml. [hash] is replaced by MD5 hash of test results XML. This enables support of parallel execution of multiple junit-xml-mocha-reporter's writing test results in separate files. In addition to this these placeholders can also be used:

placeholderoutput
[testSuitesTitle]will be replaced by the testSuitesTitle setting
[rootSuiteTitle]will be replaced by the rootSuiteTitle setting
[suiteFilename]will be replaced by the filename of the spec file
[suiteName]will be replaced by the name the first test suite

In order to display full suite title (including parents) just specify testSuitesTitle option

const mocha = new Mocha({
    reporter: 'junit-xml-mocha-reporter',
    reporterOptions: {
        testSuitesTitle: true,
    }
});

You can also configure the testsuites.name attribute by setting reporterOptions.testSuitesTitle and the root suite's name attribute by setting reporterOptions.rootSuiteTitle.

Configuration options

ParameterDefaultEffect
mochaFileresults.xmlConfigures the file to write reports to
jiraIdnullA key to check if testcaase titles contain jira ids with the key provided.
propertiesnullA hash of additional properties to add to each test suite
rootSuiteTitleRoot SuiteThe name for the root suite. (defaults to 'Root Suite')
testSuitesTitleMocha TestsThe name for the testsuites tag (defaults to 'Mocha Tests')

Debug mode

If you need see the log when it's executed you can provide the DEBUG envionment variable.

DEBUG=true

License

MIT, see LICENSE for details.