/
jsonReporter.test.ts
105 lines (88 loc) · 3.49 KB
/
jsonReporter.test.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import * as fs from 'graceful-fs';
import type {FormattedTestResults} from '@jest/test-result';
import runJest from '../runJest';
describe('JSON Reporter', () => {
const outputFileName = 'sum.result.json';
const outputFilePath = path.join(
process.cwd(),
'e2e/json-reporter/',
outputFileName,
);
afterAll(() => {
fs.unlinkSync(outputFilePath);
});
it('writes test result to sum.result.json', () => {
let jsonResult: FormattedTestResults;
runJest('json-reporter', ['--json', `--outputFile=${outputFileName}`]);
const testOutput = fs.readFileSync(outputFilePath, 'utf8');
try {
jsonResult = JSON.parse(testOutput);
} catch (err: any) {
throw new Error(
`Can't parse the JSON result from ${outputFileName}, ${err.toString()}`,
);
}
expect(jsonResult.numTotalTests).toBe(3);
expect(jsonResult.numTotalTestSuites).toBe(1);
expect(jsonResult.numRuntimeErrorTestSuites).toBe(0);
expect(jsonResult.numPassedTests).toBe(2);
expect(jsonResult.numFailedTests).toBe(1);
expect(jsonResult.numPendingTests).toBe(0);
const noAncestors = jsonResult.testResults[0].assertionResults.find(
item => item.title == 'no ancestors',
);
let expected = {ancestorTitles: [] as Array<string>};
expect(noAncestors).toEqual(expect.objectContaining(expected));
const addsNumbers = jsonResult.testResults[0].assertionResults.find(
item => item.title == 'adds numbers',
);
expected = {ancestorTitles: ['sum']};
expect(addsNumbers).toEqual(expect.objectContaining(expected));
const failsTheTest = jsonResult.testResults[0].assertionResults.find(
item => item.title == 'fails the test',
);
expected = {ancestorTitles: ['sum', 'failing tests']};
expect(failsTheTest).toEqual(expect.objectContaining(expected));
});
it('outputs coverage report', () => {
const result = runJest('json-reporter', ['--json']);
let jsonResult: FormattedTestResults;
expect(result.stderr).toMatch(/1 failed, 2 passed/);
expect(result.exitCode).toBe(1);
try {
jsonResult = JSON.parse(result.stdout);
} catch (err: any) {
throw new Error(
"Can't parse the JSON result from stdout" + err.toString(),
);
}
expect(jsonResult.numTotalTests).toBe(3);
expect(jsonResult.numTotalTestSuites).toBe(1);
expect(jsonResult.numRuntimeErrorTestSuites).toBe(0);
expect(jsonResult.numPassedTests).toBe(2);
expect(jsonResult.numFailedTests).toBe(1);
expect(jsonResult.numPendingTests).toBe(0);
const noAncestors = jsonResult.testResults[0].assertionResults.find(
item => item.title == 'no ancestors',
);
let expected = {ancestorTitles: [] as Array<string>};
expect(noAncestors).toEqual(expect.objectContaining(expected));
const addsNumbers = jsonResult.testResults[0].assertionResults.find(
item => item.title == 'adds numbers',
);
expected = {ancestorTitles: ['sum']};
expect(addsNumbers).toEqual(expect.objectContaining(expected));
const failsTheTest = jsonResult.testResults[0].assertionResults.find(
item => item.title == 'fails the test',
);
expected = {ancestorTitles: ['sum', 'failing tests']};
expect(failsTheTest).toEqual(expect.objectContaining(expected));
});
});