@villedemontreal/mocha-jenkins-reporter 0.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +112 -0
- package/index.js +1 -0
- package/lib/jenkins.js +405 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2022 Juho Vähä-Herttua and contributors
|
|
4
|
+
Copyright (c) 2013-2014 Futurice Ltd and contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
Mocha Jenkins Reporter
|
|
2
|
+
======================
|
|
3
|
+
|
|
4
|
+
This 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
|
+
|
|
6
|
+
The `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
|
+
|
|
9
|
+
General Information
|
|
10
|
+
-------------------
|
|
11
|
+
|
|
12
|
+
First 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.8"`
|
|
15
|
+
|
|
16
|
+
For 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
|
+
|
|
24
|
+
The 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
|
+
|
|
26
|
+
The environment variable `JUNIT_REPORT_NAME` is used for giving an optional name for testsuites, defaults to "Mocha Tests".
|
|
27
|
+
|
|
28
|
+
The environment variable `JUNIT_REPORT_STACK` is used to enable writing the stack trace for failed tests.
|
|
29
|
+
|
|
30
|
+
The environment variable `JUNIT_REPORT_PACKAGES` is used to enable package name to be represented by relative path to test.
|
|
31
|
+
|
|
32
|
+
Example 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
|
+
|
|
41
|
+
Programmatic Configuration
|
|
42
|
+
--------------------------
|
|
43
|
+
All of the above config values can be set with options passed in to mocha.
|
|
44
|
+
```
|
|
45
|
+
mocha({
|
|
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
|
+
|
|
55
|
+
Jenkins Setup
|
|
56
|
+
-------------
|
|
57
|
+
|
|
58
|
+
If 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
|
+
```
|
|
61
|
+
cd $WORKSPACE
|
|
62
|
+
npm install
|
|
63
|
+
npm run test-jenkins
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Make 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
|
+
|
|
68
|
+
After 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
|
+
|
|
70
|
+
After 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
|
+
|
|
72
|
+
Enabling Jenkins Screenshots
|
|
73
|
+
----------------------------
|
|
74
|
+
|
|
75
|
+
Jenkins 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
|
+
|
|
79
|
+
Screenshot extension defaults to ".png", but can also be passed in with the `imagetype` reporterOption.
|
|
80
|
+
|
|
81
|
+
SonarQube 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
|
+
|
|
86
|
+
One 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
|
+
|
|
88
|
+
License
|
|
89
|
+
-------
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
Copyright (c) 2015-2022 Juho Vähä-Herttua and contributors
|
|
93
|
+
Copyright (c) 2013-2014 Futurice Ltd and contributors
|
|
94
|
+
|
|
95
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
96
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
97
|
+
in the Software without restriction, including without limitation the rights
|
|
98
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
99
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
100
|
+
furnished to do so, subject to the following conditions:
|
|
101
|
+
|
|
102
|
+
The above copyright notice and this permission notice shall be included in
|
|
103
|
+
all copies or substantial portions of the Software.
|
|
104
|
+
|
|
105
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
106
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
107
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
108
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
109
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
110
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
111
|
+
THE SOFTWARE.
|
|
112
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/jenkins')
|
package/lib/jenkins.js
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module dependencies.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
var Base = require('mocha').reporters.Base
|
|
6
|
+
, color = Base.color
|
|
7
|
+
, fs = require('fs')
|
|
8
|
+
, path = require('path')
|
|
9
|
+
, diff= require('diff')
|
|
10
|
+
, mkdirp = require('mkdirp')
|
|
11
|
+
, util = require('util')
|
|
12
|
+
, xml = require('xml');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Save timer references to avoid Sinon interfering (see GH-237).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
var Date = global.Date;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Save original console.log.
|
|
23
|
+
*/
|
|
24
|
+
var log = console.log.bind(console);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Expose `Jenkins`.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
exports = module.exports = Jenkins;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initialize a new `Jenkins` test reporter.
|
|
34
|
+
*
|
|
35
|
+
* @param {Runner} runner
|
|
36
|
+
* @api public
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
function Jenkins(runner, options) {
|
|
40
|
+
Base.call(this, runner);
|
|
41
|
+
var self = this;
|
|
42
|
+
var fd, currentSuite;
|
|
43
|
+
var jsonResults = {};
|
|
44
|
+
|
|
45
|
+
options = (options && options.reporterOptions) || {};
|
|
46
|
+
|
|
47
|
+
// Default options
|
|
48
|
+
options.junit_report_stack = process.env.JUNIT_REPORT_STACK || options.junit_report_stack;
|
|
49
|
+
options.junit_report_path = process.env.JUNIT_REPORT_PATH || options.junit_report_path;
|
|
50
|
+
options.junit_report_name = process.env.JUNIT_REPORT_NAME || options.junit_report_name;
|
|
51
|
+
options.junit_report_packages = process.env.JUNIT_REPORT_PACKAGES || options.junit_report_packages;
|
|
52
|
+
options.jenkins_reporter_enable_sonar = process.env.JENKINS_REPORTER_ENABLE_SONAR || options.jenkins_reporter_enable_sonar;
|
|
53
|
+
options.jenkins_reporter_test_dir = process.env.JENKINS_REPORTER_TEST_DIR || options.jenkins_reporter_test_dir || 'test';
|
|
54
|
+
|
|
55
|
+
// From http://stackoverflow.com/a/961504 modified for JavaScript
|
|
56
|
+
function removeInvalidXmlChars(str) {
|
|
57
|
+
// filter-out null/undefined/etc
|
|
58
|
+
if (!str || !str.replace) return '';
|
|
59
|
+
// Remove invalid surrogate low bytes first, no lookbehind in JS :(
|
|
60
|
+
// Should be equal to str.replace(/(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, '')
|
|
61
|
+
str = str.replace(/([^\ud800-\udbff])[\udc00-\udfff]|^[\udc00-\udfff]/g, '$1');
|
|
62
|
+
// Remove other characters that are not valid for XML documents
|
|
63
|
+
// eslint-disable-next-line no-control-regex
|
|
64
|
+
return str.replace(/[\ud800-\udbff](?![\udc00-\udfff])|[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\ufeff\ufffe\uffff]/g, '');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function genSuiteReport() {
|
|
68
|
+
var testCount = currentSuite.failures+currentSuite.passes;
|
|
69
|
+
if (currentSuite.tests.length > testCount) {
|
|
70
|
+
// we have some skipped suites included
|
|
71
|
+
testCount = currentSuite.tests.length;
|
|
72
|
+
}
|
|
73
|
+
if (testCount === 0) {
|
|
74
|
+
// no tests, we can safely skip printing this suite
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (options.screenshots) {
|
|
79
|
+
var imagestring = options.imagestring || htmlEscape(currentSuite.suite.fullTitle());
|
|
80
|
+
var imagetype = options.imagetype || 'png';
|
|
81
|
+
if (options.screenshots == 'loop') {
|
|
82
|
+
var screenshotIndex = 0;
|
|
83
|
+
var screenshots = [];
|
|
84
|
+
var screenshot = '';
|
|
85
|
+
var files = fs.readdirSync(options.junit_report_path).sort();
|
|
86
|
+
for(var i in files) {
|
|
87
|
+
if (files[i].indexOf(imagestring)>-1){
|
|
88
|
+
screenshots.push(files[i]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
var testSuite = {
|
|
95
|
+
'testsuite': [{
|
|
96
|
+
_attr: {
|
|
97
|
+
name: currentSuite.suite.fullTitle(),
|
|
98
|
+
tests: testCount,
|
|
99
|
+
errors: 0, /* not supported */
|
|
100
|
+
failures: currentSuite.failures,
|
|
101
|
+
skipped: testCount-currentSuite.failures-currentSuite.passes,
|
|
102
|
+
timestamp: currentSuite.start.toISOString().slice(0, -5),
|
|
103
|
+
time: currentSuite.duration/1000
|
|
104
|
+
}
|
|
105
|
+
}]
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
var tests = currentSuite.tests;
|
|
109
|
+
|
|
110
|
+
if (tests.length === 0 && currentSuite.failures > 0) {
|
|
111
|
+
// Get the runnable that failed, which is a beforeAll or beforeEach
|
|
112
|
+
tests = [currentSuite.suite.ctx.runnable()];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
tests.forEach(function(test) {
|
|
116
|
+
var testCase = {
|
|
117
|
+
'testcase': [{
|
|
118
|
+
_attr: {
|
|
119
|
+
classname: getClassName(test, currentSuite.suite),
|
|
120
|
+
name: test.title,
|
|
121
|
+
time: 0.000
|
|
122
|
+
}
|
|
123
|
+
}]
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
if (test.duration) {
|
|
127
|
+
testCase.testcase[0]['_attr'].time = test.duration/1000;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (test.state == "failed") {
|
|
131
|
+
testCase.testcase.push({
|
|
132
|
+
'failure': [
|
|
133
|
+
{_attr: {message: removeInvalidXmlChars(test.err.message) || ''}},
|
|
134
|
+
unifiedDiff(test.err)
|
|
135
|
+
]
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
//screenshot name is either pulled in sorted order from junit_report_path
|
|
139
|
+
//or set as suitename + classname + title, then written with Jenkins ATTACHMENT tag
|
|
140
|
+
if (options.screenshots) {
|
|
141
|
+
var screenshotDir = path.join(process.cwd(), options.junit_report_path);
|
|
142
|
+
if (options.screenshots == 'loop') {
|
|
143
|
+
screenshot = path.join(screenshotDir, screenshots[screenshotIndex]);
|
|
144
|
+
screenshotIndex++;
|
|
145
|
+
} else {
|
|
146
|
+
screenshot = path.join(screenshotDir, imagestring +
|
|
147
|
+
getClassName(test, currentSuite.suite) + test.title + "." + imagetype);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
testCase.testcase.push({'system-out': ['[[ATTACHMENT|' + screenshot + ']]']})
|
|
151
|
+
}
|
|
152
|
+
} else if(test.state == 'pending') {
|
|
153
|
+
testCase.testcase.push({skipped: {}});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (test.logEntries && test.logEntries.length) {
|
|
157
|
+
var systemOut = '';
|
|
158
|
+
test.logEntries.forEach(function (entry) {
|
|
159
|
+
var outstr = util.format.apply(util, entry) + '\n';
|
|
160
|
+
outstr = removeInvalidXmlChars(outstr);
|
|
161
|
+
// We need to escape CDATA ending tags inside CDATA
|
|
162
|
+
outstr = outstr.replace(/]]>/g, ']]]]><![CDATA[>');
|
|
163
|
+
systemOut += outstr;
|
|
164
|
+
});
|
|
165
|
+
testCase.testcase.push({'system-out': {_cdata: systemOut}});
|
|
166
|
+
}
|
|
167
|
+
testSuite.testsuite.push(testCase);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
jsonResults.testsuites.push(testSuite);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function startSuite(suite) {
|
|
174
|
+
if (suite.tests && suite.tests.length === 0)
|
|
175
|
+
return;
|
|
176
|
+
|
|
177
|
+
currentSuite = {
|
|
178
|
+
suite: suite,
|
|
179
|
+
tests: [],
|
|
180
|
+
start: new Date,
|
|
181
|
+
failures: 0,
|
|
182
|
+
passes: 0
|
|
183
|
+
};
|
|
184
|
+
log();
|
|
185
|
+
log(" "+suite.fullTitle());
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function endSuite() {
|
|
189
|
+
if (currentSuite != null) {
|
|
190
|
+
currentSuite.duration = new Date - currentSuite.start;
|
|
191
|
+
log();
|
|
192
|
+
log(' Suite duration: '+(currentSuite.duration/1000)+' s, Tests: '+currentSuite.tests.length);
|
|
193
|
+
try {
|
|
194
|
+
genSuiteReport();
|
|
195
|
+
} catch (err) { log(err) }
|
|
196
|
+
currentSuite = null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function addTestToSuite(test) {
|
|
201
|
+
currentSuite.tests.push(test);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function indent() {
|
|
205
|
+
return " ";
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function htmlEscape(str) {
|
|
209
|
+
return String(str)
|
|
210
|
+
.replace(/&/g, '&')
|
|
211
|
+
.replace(/"/g, '"')
|
|
212
|
+
.replace(/'/g, ''')
|
|
213
|
+
.replace(/</g, '<')
|
|
214
|
+
.replace(/>/g, '>');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function unifiedDiff(err) {
|
|
218
|
+
function escapeInvisibles(line) {
|
|
219
|
+
return line.replace(/\t/g, '<tab>')
|
|
220
|
+
.replace(/\r/g, '<CR>')
|
|
221
|
+
.replace(/\n/g, '<LF>\n');
|
|
222
|
+
}
|
|
223
|
+
function cleanUp(line) {
|
|
224
|
+
// eslint-disable-next-line no-useless-escape
|
|
225
|
+
if (line.match(/\@\@/)) return null;
|
|
226
|
+
if (line.match(/\\ No newline/)) return null;
|
|
227
|
+
return escapeInvisibles(line);
|
|
228
|
+
}
|
|
229
|
+
function notBlank(line) {
|
|
230
|
+
return line != null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
var actual = err.actual,
|
|
234
|
+
expected = err.expected;
|
|
235
|
+
|
|
236
|
+
var lines, msg = '';
|
|
237
|
+
|
|
238
|
+
if (err.actual && err.expected) {
|
|
239
|
+
// make sure actual and expected are strings
|
|
240
|
+
if (!(typeof actual === 'string' || actual instanceof String)) {
|
|
241
|
+
actual = JSON.stringify(err.actual);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!(typeof expected === 'string' || expected instanceof String)) {
|
|
245
|
+
expected = JSON.stringify(err.expected);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (!(typeof actual === 'string' || actual instanceof String)) {
|
|
249
|
+
actual = String(actual);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (!(typeof expected === 'string' || expected instanceof String)) {
|
|
253
|
+
expected = String(expected);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
var diffstr = diff.createPatch('string', actual, expected);
|
|
257
|
+
lines = diffstr.split('\n').splice(4);
|
|
258
|
+
msg += lines.map(cleanUp).filter(notBlank).join('\n');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (options.junit_report_stack && err.stack) {
|
|
262
|
+
if (msg) msg += '\n';
|
|
263
|
+
lines = err.stack.split('\n').slice(1);
|
|
264
|
+
msg += lines.map(cleanUp).filter(notBlank).join('\n');
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return msg;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function getRelativePath(test) {
|
|
271
|
+
var relativeTestDir = options.jenkins_reporter_test_dir,
|
|
272
|
+
absoluteTestDir = path.join(process.cwd(), relativeTestDir);
|
|
273
|
+
return path.relative(absoluteTestDir, test.file || '');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function getClassName(test, suite) {
|
|
277
|
+
if (options.jenkins_reporter_enable_sonar) {
|
|
278
|
+
// Inspired by https://github.com/pghalliday/mocha-sonar-reporter
|
|
279
|
+
var relativeFilePath = getRelativePath(test),
|
|
280
|
+
fileExt = path.extname(relativeFilePath);
|
|
281
|
+
return relativeFilePath.replace(new RegExp(fileExt+"$"), '');
|
|
282
|
+
}
|
|
283
|
+
if (options.junit_report_packages) {
|
|
284
|
+
// eslint-disable-next-line no-useless-escape
|
|
285
|
+
var testPackage = getRelativePath(test).replace(/[^\/]*$/, ''),
|
|
286
|
+
delimiter = testPackage ? '.' : '';
|
|
287
|
+
return testPackage + delimiter + suite.fullTitle();
|
|
288
|
+
}
|
|
289
|
+
if (options.junit_report_name) {
|
|
290
|
+
return options.junit_report_name + '.' + suite.fullTitle();
|
|
291
|
+
}
|
|
292
|
+
return suite.fullTitle();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
runner.on('start', function() {
|
|
296
|
+
var reportPath = options.junit_report_path;
|
|
297
|
+
var suitesName = options.junit_report_name || 'Mocha Tests';
|
|
298
|
+
if (reportPath) {
|
|
299
|
+
try {
|
|
300
|
+
if (fs.existsSync(reportPath)) {
|
|
301
|
+
var isDirectory = fs.statSync(reportPath).isDirectory();
|
|
302
|
+
if (isDirectory) reportPath = path.join(reportPath, new Date().getTime() + ".xml");
|
|
303
|
+
} else {
|
|
304
|
+
mkdirp.sync(path.dirname(reportPath));
|
|
305
|
+
}
|
|
306
|
+
fd = fs.openSync(reportPath, 'w');
|
|
307
|
+
} catch {
|
|
308
|
+
// Not much we can do except logging this
|
|
309
|
+
log('WARNING: Could not open report file ' + reportPath + ', running without report');
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
jsonResults = {
|
|
313
|
+
'testsuites': [
|
|
314
|
+
{_attr: {name: suitesName}}
|
|
315
|
+
]
|
|
316
|
+
};
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
runner.on('end', function() {
|
|
320
|
+
endSuite();
|
|
321
|
+
if (fd) {
|
|
322
|
+
fs.writeSync(fd, xml(jsonResults, {indent: ' '}));
|
|
323
|
+
fs.closeSync(fd);
|
|
324
|
+
fd = null;
|
|
325
|
+
}
|
|
326
|
+
try {
|
|
327
|
+
self.epilogue.call(self);
|
|
328
|
+
} catch {
|
|
329
|
+
// Epilogue is not considered critical, fail silently
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
runner.on('suite', function (suite) {
|
|
334
|
+
if (currentSuite) {
|
|
335
|
+
endSuite();
|
|
336
|
+
}
|
|
337
|
+
startSuite(suite);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
runner.on('test', function (test) {
|
|
341
|
+
test.logEntries = [];
|
|
342
|
+
console.log = function () {
|
|
343
|
+
log.apply(this, arguments);
|
|
344
|
+
test.logEntries.push(Array.prototype.slice.call(arguments));
|
|
345
|
+
};
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
runner.on('test end', function(/*test*/) {
|
|
349
|
+
console.log = log;
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
runner.on('pending', function(test) {
|
|
353
|
+
var fmt = indent()
|
|
354
|
+
+ color('checkmark', ' -')
|
|
355
|
+
+ color('pending', ' %s');
|
|
356
|
+
log(fmt, test.title);
|
|
357
|
+
test.state = test.state || 'pending';
|
|
358
|
+
addTestToSuite(test);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
runner.on('pass', function(test) {
|
|
362
|
+
currentSuite.passes++;
|
|
363
|
+
var fmt = indent()
|
|
364
|
+
+ color('checkmark', ' '+Base.symbols.ok)
|
|
365
|
+
+ color('pass', ' %s: ')
|
|
366
|
+
+ color(test.speed, '%dms');
|
|
367
|
+
log(fmt, test.title, test.duration);
|
|
368
|
+
addTestToSuite(test);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
runner.on('fail', function(test, err) {
|
|
372
|
+
if (currentSuite == undefined) {
|
|
373
|
+
// Failure occurred outside of a test suite.
|
|
374
|
+
startSuite({
|
|
375
|
+
tests: ["other"],
|
|
376
|
+
fullTitle: function() { return "Non-test failures"; }
|
|
377
|
+
});
|
|
378
|
+
var n = ++currentSuite.failures;
|
|
379
|
+
var fmt = indent()
|
|
380
|
+
+ color('fail', ' %d) %s');
|
|
381
|
+
if (test == undefined) {
|
|
382
|
+
log(fmt, n, "unknown");
|
|
383
|
+
addTestToSuite({
|
|
384
|
+
title: "unknown",
|
|
385
|
+
file: process.cwd() + "/other.js",
|
|
386
|
+
state: 'failed',
|
|
387
|
+
err: err
|
|
388
|
+
});
|
|
389
|
+
} else {
|
|
390
|
+
log(fmt, n, test.title);
|
|
391
|
+
addTestToSuite(test);
|
|
392
|
+
}
|
|
393
|
+
endSuite();
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
n = ++currentSuite.failures;
|
|
398
|
+
fmt = indent()
|
|
399
|
+
+ color('fail', ' %d) %s');
|
|
400
|
+
log(fmt, n, test.title);
|
|
401
|
+
addTestToSuite(test);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
Jenkins.prototype.__proto__ = Base.prototype;
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@villedemontreal/mocha-jenkins-reporter",
|
|
3
|
+
"version": "0.4.9",
|
|
4
|
+
"description": "jenkins reporter for mocha",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"directories": "./lib",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"compile": "echo \"No need to compile\"",
|
|
12
|
+
"lint": "eslint lib/jenkins.js",
|
|
13
|
+
"lint-fix": "eslint lib/jenkins.js --fix",
|
|
14
|
+
"test": "echo \"Error: no test specified\"",
|
|
15
|
+
"ncu": "ncu --target minor -x @types/node",
|
|
16
|
+
"ncu-fix": "ncu -u --target minor -x @types/node"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/villedemontreal/node-core-libs.git",
|
|
21
|
+
"directory": "packages/fork-mocha-jenkins-reporter"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/villedemontreal/node-core-libs",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"mocha",
|
|
26
|
+
"jenkins",
|
|
27
|
+
"reporter"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"diff": "8.0.2",
|
|
31
|
+
"mkdirp": "^3.0.1",
|
|
32
|
+
"xml": "^1.0.1"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"mocha": "^5.2.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0"
|
|
36
|
+
},
|
|
37
|
+
"author": "Juho Vähä-Herttua",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/juhovh/mocha-jenkins-reporter/issues"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@eslint/js": "^9.38.0",
|
|
44
|
+
"eslint": "^9.38.0",
|
|
45
|
+
"globals": "^16.4.0"
|
|
46
|
+
}
|
|
47
|
+
}
|