-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathbuildJsonResults.js
338 lines (290 loc) · 10.6 KB
/
buildJsonResults.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
'use strict';
const stripAnsi = require('strip-ansi');
const constants = require('../constants/index');
const path = require('path');
const fs = require('fs');
const getTestSuitePropertiesPath = require('./getTestSuitePropertiesPath');
const replaceRootDirInOutput = require('./getOptions').replaceRootDirInOutput;
// Wrap the varName with template tags
const toTemplateTag = function (varName) {
return "{" + varName + "}";
}
const testFailureStatus = 'failed';
const testErrorStatus = 'error';
// Replaces var using a template string or a function.
// When strOrFunc is a template string replaces {varname} with the value from the variables map.
// When strOrFunc is a function it returns the result of the function to which the variables are passed.
const replaceVars = function (strOrFunc, variables) {
if (typeof strOrFunc === 'string') {
let str = strOrFunc;
Object.keys(variables).forEach((varName) => {
str = str.replace(toTemplateTag(varName), variables[varName]);
});
return str;
} else {
const func = strOrFunc;
const resolvedStr = func(variables);
if (typeof resolvedStr !== 'string') {
throw new Error('Template function should return a string');
}
return resolvedStr;
}
};
const executionTime = function (startTime, endTime) {
return (endTime - startTime) / 1000;
}
const getTestCasePropertiesPath = (options, rootDir = null) => {
const testCasePropertiesPath = replaceRootDirInOutput(
rootDir,
path.join(
options.testCasePropertiesDirectory,
options.testCasePropertiesFile,
),
);
return path.isAbsolute(testCasePropertiesPath)
? testCasePropertiesPath
: path.resolve(testCasePropertiesPath);
};
const generateTestCase = function(junitOptions, suiteOptions, tc, filepath, filename, suiteTitle, displayName, getGetCaseProperties){
const classname = tc.ancestorTitles.join(suiteOptions.ancestorSeparator);
const testTitle = tc.title;
// Build replacement map
let testVariables = {};
testVariables[constants.FILEPATH_VAR] = filepath;
testVariables[constants.FILENAME_VAR] = filename;
testVariables[constants.SUITENAME_VAR] = suiteTitle;
testVariables[constants.CLASSNAME_VAR] = classname;
testVariables[constants.TITLE_VAR] = testTitle;
testVariables[constants.DISPLAY_NAME_VAR] = displayName;
let testCase = {
'testcase': [{
_attr: {
classname: replaceVars(suiteOptions.classNameTemplate, testVariables),
name: replaceVars(suiteOptions.titleTemplate, testVariables),
time: tc.duration / 1000
}
}]
};
if (suiteOptions.addFileAttribute === 'true') {
testCase.testcase[0]._attr.file = filepath;
}
// Write out all failure messages as <failure> tags
// Nested underneath <testcase> tag
if (tc.status === testFailureStatus || tc.status === testErrorStatus) {
const failureMessages = junitOptions.noStackTrace === 'true' && tc.failureDetails ?
tc.failureDetails.map(detail => detail.message) : tc.failureMessages;
failureMessages.forEach((failure) => {
const tagName = tc.status === testFailureStatus ? 'failure': testErrorStatus
testCase.testcase.push({
[tagName]: strip(failure)
});
})
}
// Write out a <skipped> tag if test is skipped
// Nested underneath <testcase> tag
if (tc.status === 'pending') {
testCase.testcase.push({
skipped: {}
});
}
if (getGetCaseProperties !== null) {
let junitCaseProperties = getGetCaseProperties(tc);
// Add any test suite properties
let testCasePropertyMain = {
'properties': []
};
Object.keys(junitCaseProperties).forEach((p) => {
let testSuiteProperty = {
'property': {
_attr: {
name: p,
value: junitCaseProperties[p]
}
}
};
testCasePropertyMain.properties.push(testSuiteProperty);
});
testCase.testcase.push(testCasePropertyMain);
}
return testCase;
}
const addErrorTestResult = function (suite) {
suite.testResults.push({
"ancestorTitles": [],
"duration": 0,
"failureMessages": [
suite.failureMessage
],
"numPassingAsserts": 0,
"status": testErrorStatus
})
}
// Strips escape codes for readability and illegal XML characters to produce valid output.
const strip = function (str) {
return stripAnsi(str).replace(/\u001b/g, '');
}
module.exports = function (report, appDirectory, options, rootDir = null) {
// Check if there is a junitProperties.js (or whatever they called it)
const junitSuitePropertiesFilePath = getTestSuitePropertiesPath(
options,
rootDir,
);
let ignoreSuitePropertiesCheck = !fs.existsSync(junitSuitePropertiesFilePath);
const testCasePropertiesPath = getTestCasePropertiesPath(options, rootDir)
const getTestCaseProperties = fs.existsSync(testCasePropertiesPath) ? require(testCasePropertiesPath) : null
// If the usePathForSuiteName option is true and the
// suiteNameTemplate value is set to the default, overrides
// the suiteNameTemplate.
if (options.usePathForSuiteName === 'true' &&
options.suiteNameTemplate === toTemplateTag(constants.TITLE_VAR)) {
options.suiteNameTemplate = toTemplateTag(constants.FILEPATH_VAR);
}
// Generate a single XML file for all jest tests
let jsonResults = {
'testsuites': [{
'_attr': {
'name': options.suiteName,
'tests': 0,
'failures': 0,
'errors': 0,
// Overall execution time:
// Since tests are typically executed in parallel this time can be significantly smaller
// than the sum of the individual test suites
'time': executionTime(report.startTime, Date.now())
}
}]
};
// Iterate through outer testResults (test suites)
report.testResults.forEach((suite) => {
const noResults = suite.testResults.length === 0;
if (noResults && options.reportTestSuiteErrors === 'false') {
return;
}
const noResultOptions = noResults ? {
suiteNameTemplate: toTemplateTag(constants.FILEPATH_VAR),
titleTemplate: toTemplateTag(constants.FILEPATH_VAR),
classNameTemplate: `Test suite failed to run`
} : {};
const suiteOptions = Object.assign({}, options, noResultOptions);
if (noResults) {
addErrorTestResult(suite);
}
// Build variables for suite name
const filepath = path.join(suiteOptions.filePathPrefix, path.relative(appDirectory, suite.testFilePath));
const filename = path.basename(filepath);
const suiteTitle = suite.testResults[0].ancestorTitles[0];
const displayName = typeof suite.displayName === 'object'
? suite.displayName.name
: suite.displayName;
// Build replacement map
let suiteNameVariables = {};
suiteNameVariables[constants.FILEPATH_VAR] = filepath;
suiteNameVariables[constants.FILENAME_VAR] = filename;
suiteNameVariables[constants.TITLE_VAR] = suiteTitle;
suiteNameVariables[constants.DISPLAY_NAME_VAR] = displayName;
// Add <testsuite /> properties
const suiteNumTests = suite.numFailingTests + suite.numPassingTests + suite.numPendingTests;
const suiteExecutionTime = executionTime(suite.perfStats.start, suite.perfStats.end);
const suiteErrors = noResults ? 1 : 0;
let testSuite = {
'testsuite': [{
_attr: {
name: replaceVars(suiteOptions.suiteNameTemplate, suiteNameVariables),
errors: suiteErrors,
failures: suite.numFailingTests,
skipped: suite.numPendingTests,
timestamp: (new Date(suite.perfStats.start)).toISOString().slice(0, -5),
time: suiteExecutionTime,
tests: suiteNumTests
}
}]
};
// Update top level testsuites properties
jsonResults.testsuites[0]._attr.failures += suite.numFailingTests;
jsonResults.testsuites[0]._attr.errors += suiteErrors;
jsonResults.testsuites[0]._attr.tests += suiteNumTests;
if (!ignoreSuitePropertiesCheck) {
let junitSuiteProperties = require(junitSuitePropertiesFilePath)(suite);
// Add any test suite properties
let testSuitePropertyMain = {
'properties': []
};
Object.keys(junitSuiteProperties).forEach((p) => {
let testSuiteProperty = {
'property': {
_attr: {
name: p,
value: replaceVars(junitSuiteProperties[p], suiteNameVariables)
}
}
};
testSuitePropertyMain.properties.push(testSuiteProperty);
});
testSuite.testsuite.push(testSuitePropertyMain);
}
// Iterate through test cases
suite.testResults.forEach((tc) => {
const testCase = generateTestCase(
options,
suiteOptions,
tc,
filepath,
filename,
suiteTitle,
displayName,
getTestCaseProperties
);
testSuite.testsuite.push(testCase);
});
// We have all tests passed but a failure in a test hook like in the `beforeAll` method
// Make sure we log them since Jest still reports the suite as failed
if (suite.testExecError != null) {
const fakeTC = {
status: testFailureStatus,
failureMessages: [JSON.stringify(suite.testExecError)],
classname: undefined,
title: "Test execution failure: could be caused by test hooks like 'afterAll'.",
ancestorTitles: [""],
duration: 0,
invocations: 1,
};
const testCase = generateTestCase(
options,
suiteOptions,
fakeTC,
filepath,
filename,
suiteTitle,
displayName,
getTestCaseProperties
);
testSuite.testsuite.push(testCase);
}
// Write stdout console output if available
if (suiteOptions.includeConsoleOutput === 'true' && suite.console && suite.console.length) {
// Stringify the entire console object
// Easier this way because formatting in a readable way is tough with XML
// And this can be parsed more easily
let testSuiteConsole = {
'system-out': {
_cdata: JSON.stringify(suite.console, null, 2)
}
};
testSuite.testsuite.push(testSuiteConsole);
}
// Write short stdout console output if available
if (suiteOptions.includeShortConsoleOutput === 'true' && suite.console && suite.console.length) {
// Extract and then Stringify the console message value
// Easier this way because formatting in a readable way is tough with XML
// And this can be parsed more easily
let testSuiteConsole = {
'system-out': {
_cdata: JSON.stringify(suite.console.map(item => item.message), null, 2)
}
};
testSuite.testsuite.push(testSuiteConsole);
}
jsonResults.testsuites.push(testSuite);
});
return jsonResults;
};