/
testResultProcessor.test.js
63 lines (49 loc) · 1.71 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
'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('libxmljs');
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 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(/junit-\S+\.xml/), expect.any(String));
// Ensure generated file is valid xml
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
expect(xmlDoc).toBeTruthy();
});
});