-
Notifications
You must be signed in to change notification settings - Fork 104
/
testResultProcessor.test.js
97 lines (73 loc) · 3 KB
/
testResultProcessor.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
'use strict';
jest.mock('mkdirp', () => {
return Object.assign(
{},
jest.requireActual('mkdirp'),
{
sync: jest.fn()
}
)
});
jest.mock('fs', () => {
return Object.assign(
{},
jest.requireActual('fs'),
{
writeFileSync: jest.fn()
}
)
});
const fs = require('fs');
const libxmljs = require('libxmljs2');
const path = require('path');
const testResultProcessor = require('../');
describe('jest-junit', () => {
afterEach(() => jest.clearAllMocks())
it('should generate valid xml with default name', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport);
// Ensure fs.writeFileSync is called
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(path.resolve('junit.xml'), expect.any(String));
// Ensure generated file is valid xml
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
expect(xmlDoc).toBeTruthy();
});
it('should generate valid xml with unique name', () => {
process.env.JEST_JUNIT_UNIQUE_OUTPUT_NAME = 'true'
const outputPrefix = "foo"
process.env.JEST_JUNIT_OUTPUT_NAME = outputPrefix;
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport);
// Ensure fs.writeFileSync is called
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
const expectedOutputRegex = new RegExp(`${outputPrefix}-\\S+.xml`, "g")
// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(expect.stringMatching(expectedOutputRegex), expect.any(String));
// Ensure generated file is valid xml
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
expect(xmlDoc).toBeTruthy();
});
it('should generate xml at the output filepath defined by JEST_JUNIT_OUTPUT_FILE', () => {
process.env.JEST_JUNIT_OUTPUT_FILE = 'path_to_output/output_name.xml'
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport);
// Ensure fs.writeFileSync is called
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(
expect.stringMatching(/path_to_output\S+\output_name.xml/), expect.any(String)
);
});
it('should generate xml at the output filepath defined by outputFile config', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport, {outputFile: 'path_to_output/output_name.xml' });
// Ensure fs.writeFileSync is called
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(
expect.stringMatching(/path_to_output\S+\output_name.xml/), expect.any(String)
);
});
});