-
Notifications
You must be signed in to change notification settings - Fork 104
/
buildJsonResults.test.js
421 lines (339 loc) · 18.5 KB
/
buildJsonResults.test.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
'use strict';
const fs = require('fs');
const slash = require('slash');
const buildJsonResults = require('../utils/buildJsonResults');
const constants = require('../constants/index');
let jsonResults;
let ignoreJunitErrors = false;
describe('buildJsonResults', () => {
beforeEach(() => {
jest.spyOn(fs, 'existsSync');
})
afterEach(() => {
jest.restoreAllMocks();
if (ignoreJunitErrors !== true) {
// Verify each tests JSON output results in a
// compliant junit.xml file based on __tests__/lib/junit.xsd (jenkins xsd)
expect(jsonResults).toBeCompliantJUnit();
}
// Reset ignoreJunitErrors
ignoreJunitErrors = false;
jsonResults = undefined;
});
it('should contain number of tests in testSuite', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/', constants.DEFAULT_OPTIONS);
expect(jsonResults.testsuites[1].testsuite[0]._attr.tests).toBe(1);
});
it('should contain number of tests in testSuites', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/', constants.DEFAULT_OPTIONS);
expect(jsonResults.testsuites[0]._attr.tests).toBe(1);
});
it('should return the proper name from ancestorTitles when usePathForSuiteName is "false"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/', constants.DEFAULT_OPTIONS);
expect(jsonResults.testsuites[1].testsuite[0]._attr.name).toBe('foo');
});
it('should return the proper filename when suiteNameTemplate is "{filename}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
suiteNameTemplate: "{filename}"
}));
expect(jsonResults.testsuites[1].testsuite[0]._attr.name).toBe('foo.test.js');
});
it('should support suiteNameTemplate as function', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
suiteNameTemplate: (vars) => {
return 'function called with vars: ' + Object.keys(vars).join(', ');
}
}));
expect(jsonResults.testsuites[1].testsuite[0]._attr.name)
.toBe('function called with vars: filepath, filename, title, displayName');
});
it('should return the proper classname when classNameTemplate is "{classname}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: "{classname}"
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('foo baz');
});
it('should return the proper title when classNameTemplate is "{title}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: "{title}"
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('should bar');
});
it('should return the proper filepath when classNameTemplate is "{filepath}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: "{filepath}"
}));
expect(slash(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname)).toBe('path/to/test/__tests__/foo.test.js');
});
it('should return the proper filename when classNameTemplate is "{filename}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: "{filename}"
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('foo.test.js');
});
it('should return the proper displayName when classNameTemplate is {displayName}', () => {
const multiProjectNoFailingTestsReport = require('../__mocks__/multi-project-no-failing-tests.json');
jsonResults = buildJsonResults(multiProjectNoFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: "{displayName}"
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('project1');
});
it('should return the proper suitename when classNameTemplate is "{suitename}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: "{suitename}"
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('foo');
});
it('should support return the function result when classNameTemplate is a function', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
classNameTemplate: (vars) => {
return 'function called with vars: ' + Object.keys(vars).join(', ');
}
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname)
.toBe('function called with vars: filepath, filename, suitename, classname, title, displayName');
});
it('should report no results as error', () => {
const failingTestsReport = require('../__mocks__/failing-compilation.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
reportTestSuiteErrors: "true"
}));
const totals = jsonResults.testsuites[0]._attr;
expect(totals.tests).toEqual(1);
expect(totals.errors).toEqual(1);
expect(totals.failures).toEqual(0);
const suiteResult = jsonResults.testsuites[1].testsuite[0]._attr;
expect(slash(suiteResult.name)).toEqual('../spec/test.spec.ts')
expect(suiteResult.errors).toEqual(1);
expect(suiteResult.tests).toEqual(0);
const errorSuite = jsonResults.testsuites[1].testsuite[2];
expect(errorSuite.testcase[0]._attr.name).toEqual(suiteResult.name);
expect(errorSuite.testcase[0]._attr.classname).toEqual('Test suite failed to run');
expect(errorSuite.testcase[1].error).toContain("Property 'hello' does not exist");
});
it('should report failureMessage if testExecErrorNotSet', () => {
const failingTestsReport = require('../__mocks__/failing-import.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
reportTestSuiteErrors: "true"
}));
const errorSuite = jsonResults.testsuites[1].testsuite[2];
expect(slash(errorSuite.testcase[0]._attr.name)).toEqual('../spec/test.spec.ts');
expect(errorSuite.testcase[0]._attr.classname).toEqual('Test suite failed to run');
expect(errorSuite.testcase[1].error).toContain("Cannot find module './mult'");
});
it('should report empty suites as error', () => {
const failingTestsReport = require('../__mocks__/empty-suite.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
reportTestSuiteErrors: "true"
}));
const errorSuite = jsonResults.testsuites[1].testsuite[2];
expect(slash(errorSuite.testcase[0]._attr.name)).toEqual('../spec/test.spec.ts');
expect(errorSuite.testcase[0]._attr.classname).toEqual('Test suite failed to run');
expect(errorSuite.testcase[1].error).toContain("Your test suite must contain at least one test");
});
it('should honor templates when test has errors', () => {
const failingTestsReport = require('../__mocks__/failing-compilation.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
reportTestSuiteErrors: "true",
suiteNameTemplate: "{displayName}-foo",
titleTemplate: "{title}-bar"
}));
expect(jsonResults.testsuites[2].testsuite[2].testcase[0]._attr.name).toEqual('should foo-bar');
});
it('should return the proper filepath when titleTemplate is "{filepath}"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
titleTemplate: "{filepath}"
}));
expect(slash(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.name)).toBe('path/to/test/__tests__/foo.test.js');
});
it('should return the proper filepath when suiteNameTemplate is "{filepath}" and usePathForSuiteName is "false"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
suiteNameTemplate: "{filepath}"
}));
expect(slash(jsonResults.testsuites[1].testsuite[0]._attr.name)).toBe('path/to/test/__tests__/foo.test.js');
});
it('should return the proper name from ancestorTitles when suiteNameTemplate is set to "{title}" and usePathForSuiteName is "true"', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
usePathForSuiteName: "true"
}));
expect(slash(jsonResults.testsuites[1].testsuite[0]._attr.name)).toBe('path/to/test/__tests__/foo.test.js');
});
it('should return the proper name from testFilePath when usePathForSuiteName is "true"; no appDirectory set', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
usePathForSuiteName: "true"
}));
expect(slash(jsonResults.testsuites[1].testsuite[0]._attr.name)).toBe('path/to/test/__tests__/foo.test.js');
});
it('should return the proper name from testFilePath when usePathForSuiteName is "true"; with appDirectory set', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
usePathForSuiteName: "true"
}));
expect(slash(jsonResults.testsuites[1].testsuite[0]._attr.name)).toBe('__tests__/foo.test.js');
});
it('should return the proper classname when ancestorSeparator is default', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('foo baz should bar');
});
it('should return the proper classname when ancestorSeparator is customized', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
ancestorSeparator: " › "
}));
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.classname).toBe('foo › baz should bar');
});
it('should parse failure messages for failing tests', () => {
const failingTestsReport = require('../__mocks__/failing-tests.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test', constants.DEFAULT_OPTIONS);
const failureMsg = jsonResults.testsuites[1].testsuite[2].testcase[1].failure;
// Make sure no escape codes are there that exist in the mock
expect(failureMsg.includes('\u001b')).toBe(false);
});
it('should parse messages without stack trace when notStackTrace set to true and jest >= 26.3.0', () => {
const failingTestsReport = require('../__mocks__/failing-tests-with-failure-details.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
noStackTrace: "true"
}));
const failureMsg = jsonResults.testsuites[1].testsuite[2].testcase[1].failure;
// Make sure no escape codes are there that exist in the mock
expect(failureMsg.includes('\u001b')).toBe(false);
expect(failureMsg).toMatch('Should fail');
expect(failureMsg).not.toMatch('at _callee$ (path/to/failing.test.js:26:15)');
expect(failureMsg).not.toMatch('at path/to/failing.test.js:2:554');
});
it('should parse messages with stack trace when notStackTrace set to false and jest >= 26.3.0', () => {
const failingTestsReport = require('../__mocks__/failing-tests-with-failure-details.json');
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
noStackTrace: "false"
}));
const failureMsg = jsonResults.testsuites[1].testsuite[2].testcase[1].failure;
// Make sure no escape codes are there that exist in the mock
expect(failureMsg.includes('\u001b')).toBe(false);
expect(failureMsg).toMatch('Should fail');
expect(failureMsg).toMatch('at _callee$ (path/to/failing.test.js:26:15)');
expect(failureMsg).toMatch('at path/to/failing.test.js:2:554');
});
it('should parse failure messages for failing tests and not crash when notStackTrace set to true and jest < 26.3.0', () => {
const failingTestsReport = require('../__mocks__/failing-tests.json'); // no failure details
jsonResults = buildJsonResults(failingTestsReport, '/path/to/test',
Object.assign({}, constants.DEFAULT_OPTIONS, {
noStackTrace: "true"
}));
const failureMsg = jsonResults.testsuites[1].testsuite[2].testcase[1].failure;
// Make sure no escape codes are there that exist in the mock
expect(failureMsg.includes('\u001b')).toBe(false);
expect(failureMsg).toMatch('Should fail');
});
it('should support displayName template var for jest multi-project', () => {
const multiProjectNoFailingTestsReport = require('../__mocks__/multi-project-no-failing-tests.json');
// Mock Date.now() to return a fixed later value
const startDate = new Date(multiProjectNoFailingTestsReport.startTime);
jest.spyOn(Date, 'now').mockImplementation(() => startDate.getTime() + 1234);
jsonResults = buildJsonResults(multiProjectNoFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
suiteNameTemplate: "{displayName}-foo",
titleTemplate: "{displayName}-bar"
}));
expect(jsonResults).toMatchSnapshot();
});
it('should not return the file name by default', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/', constants.DEFAULT_OPTIONS);
expect(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.file).toBe(undefined);
});
it('should return the file name when addFileAttribute is "true"', () => {
// Ignore junit errors for this attribute
// It is added for circle-ci and is known to not generate
// jenkins-compatible junit
ignoreJunitErrors = true;
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
addFileAttribute: "true"
}));
expect(slash(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.file)).toBe('path/to/test/__tests__/foo.test.js');
});
it('should prefix the file name with filePathPrefix', () => {
// Ignore junit errors for this attribute
// It is added for circle-ci and is known to not generate
// jenkins-compatible junit
ignoreJunitErrors = true;
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
jsonResults = buildJsonResults(noFailingTestsReport, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
addFileAttribute: "true",
filePathPrefix: "packages/foobar"
}));
expect(slash(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.file)).toBe('packages/foobar/path/to/test/__tests__/foo.test.js');
});
it('should show output of console if includeConsoleOutput is true', () => {
const reportWithConsoleOutput = require('../__mocks__/test-with-console-output.json');
jsonResults = buildJsonResults(reportWithConsoleOutput, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
includeConsoleOutput: "true"
}));
expect(jsonResults.testsuites[1].testsuite[3]['system-out']).toBeDefined();
});
it('should NOT show output of console if includeConsoleOutput is not set or false', () => {
const reportWithConsoleOutput = require('../__mocks__/test-with-console-output.json');
jsonResults = buildJsonResults(reportWithConsoleOutput, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
includeConsoleOutput: "false"
}));
expect(jsonResults.testsuites[1].testsuite[1]['system-out']).not.toBeDefined();
});
it('should show short console output if includeShortConsoleOutput is true', () => {
const reportWithShortConsoleOutput = require('../__mocks__/test-with-console-output.json');
jsonResults = buildJsonResults(reportWithShortConsoleOutput, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
includeShortConsoleOutput: "true"
}));
expect(jsonResults.testsuites[1].testsuite[3]['system-out']._cdata).toEqual("[\n \"I am bar\",\n \"Some output here from a lib\"\n]");
});
it('should NOT show short console output if includeShortConsoleOutput is not set or false', () => {
const reportWithShortConsoleOutput = require('../__mocks__/test-with-console-output.json');
jsonResults = buildJsonResults(reportWithShortConsoleOutput, '/',
Object.assign({}, constants.DEFAULT_OPTIONS, {
includeShortConsoleOutput: "false"
}));
expect(jsonResults.testsuites[1].testsuite[2]['system-out']).not.toBeDefined();
});
});