UNPKG

6.28 kBMarkdownView Raw
1Mocha Jenkins Reporter
2======================
3
4This reporter is useful if you want to run Node.js backend tests using mocha and need a nicely formatted Jenkins reports of the test runs. The existing `xunit` reporter is very similar, but doesn't make it possible to output both XML report and a console output at the same time, which would often be useful with Jenkins.
5
6The `xunit` reporter also doesn't handle separate tests suites but adds all tests to a single suite instead, this reporter instead combines nested test suites to a single suite and uses that in the reports. As a nice plus, this reporter also shows the running time of each suite separately. All the code is released under the `MIT` license which can be found from the end of this file.
7
8
9General Information
10-------------------
11
12First you need to add the library to your package.json, you can use the following setting to get the latest version:
13
14`"mocha-jenkins-reporter": "0.4.2"`
15
16For the actual test run you can add the following to package.json:
17
18```
19"scripts": {
20 "test-jenkins": "JUNIT_REPORT_PATH=/report.xml mocha --colors --reporter mocha-jenkins-reporter"
21}
22```
23
24The environment variable `JUNIT_REPORT_PATH` is used for passing the output filename or directory for the reporter. If an explicit filename is used, any existing reports in the same path will be overwritten, so be careful with it. If an existing directory is used instead, then the output will be in the format "path/to/directory/timestamp.xml" where timestamp is milliseconds since January 1, 1970. If the environment variable is not set, no JUnit style XML report is written and the test results are only printed to the console.
25
26The environment variable `JUNIT_REPORT_NAME` is used for giving an optional name for testsuites, defaults to "Mocha Tests".
27
28The environment variable `JUNIT_REPORT_STACK` is used to enable writing the stack trace for failed tests.
29
30The environment variable `JUNIT_REPORT_PACKAGES` is used to enable package name to be represented by relative path to test.
31
32Example console output of the reporter:
33
34```
35 In test suite number one
36 ․ the asynchronous test should work: 47ms
37
38 Suite duration: 0.048 s, Tests: 1
39```
40
41Programmatic Configuration
42--------------------------
43All of the above config values can be set with options passed in to mocha.
44```
45mocha({
46 "reporter": "mocha-jenkins-reporter",
47 "reporterOptions": {
48 "junit_report_name": "Tests",
49 "junit_report_path": "report.xml",
50 "junit_report_stack": 1
51 }
52 }
53```
54
55Jenkins Setup
56-------------
57
58If you use the package.json approach specified in the last section, setting up Jenkins should be pretty straighforward. For the shell execution you can use something like this:
59
60```
61cd $WORKSPACE
62npm install
63npm run test-jenkins
64```
65
66Make sure to set the `Color ANSI Console Output` on and use for example `xterm` for the `ANSI color map` setting, in order to show the output colors nicely in Jenkins.
67
68After this you should be able to add `Publish JUnit test result report` in your `Post-build Actions` and write for example `report.xml` to the `Test report XMLs` field if your package.json was exactly as above. You can use your own variations of these commands as you wish, but this should get anyone started.
69
70After all this setting up, just click `Save` and start building, you should get all errors nicely both to the console log as the tests are being run and finally to the Jenkins reports.
71
72Enabling Jenkins Screenshots
73----------------------------
74
75Jenkins screenshot attachments can be written to the report to allow for a screenshot attachment in each test failure. Simply specify a reporterOption of `spec` or `loop`. This writes a `system-out` xml element to the JUnit report, leveraging the `Publish test attachments` feature of the `JUnit Attachments Plugin`.
76
77`spec` will write the full path of the screenshot with a filename consisting of "suitename+classname+test.title+extension". `loop` pulls and sorts all screenshots with specific filename text from `JUNIT_REPORT_PATH` and writes them in order according to the names of the files pulled. The `imagestring` reporterOption can be used to specify what files to pull, allowing for custom screenshot naming conventions on the mocha side, otherwise it defaults to the test suite name.
78
79Screenshot extension defaults to ".png", but can also be passed in with the `imagetype` reporterOption.
80
81SonarQube Integration
82---------------------
83
84[SonarQube](http://www.sonarqube.org/) is a popular tool for continuous inspection of code quality. You can find documentation for JavaScript language on [JavaScript Plugin](http://docs.sonarqube.org/display/SONAR/JavaScript+Plugin) page.
85
86One aspect of SonarQube analysis is outcome of unit tests. To properly display test results environment variable `JENKINS_REPORTER_ENABLE_SONAR` must be set to `true`. By default reporter looks for tests in `./test` directory. It can be changed using environment variable `JENKINS_REPORTER_TEST_DIR` and providing relative path to the directory with tests.
87
88License
89-------
90
91```
92Copyright (c) 2015-2016 Juho Vähä-Herttua and contributors
93Copyright (c) 2013-2014 Futurice Ltd and contributors
94
95Permission is hereby granted, free of charge, to any person obtaining a copy
96of this software and associated documentation files (the "Software"), to deal
97in the Software without restriction, including without limitation the rights
98to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99copies of the Software, and to permit persons to whom the Software is
100furnished to do so, subject to the following conditions:
101
102The above copyright notice and this permission notice shall be included in
103all copies or substantial portions of the Software.
104
105THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
106IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
107FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
108AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
109LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
110OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
111THE SOFTWARE.
112```