forked from jestjs/jest
/
testRetries.test.ts
137 lines (113 loc) · 3.73 KB
/
testRetries.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
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
/**
* 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 {skipSuiteOnJasmine} from '@jest/test-utils';
import runJest from '../runJest';
skipSuiteOnJasmine();
describe('Test Retries', () => {
const outputFileName = 'retries.result.json';
const outputFilePath = path.join(
process.cwd(),
'e2e/test-retries/',
outputFileName,
);
const logErrorsBeforeRetryErrorMessage =
'Errors that caused Jest to retry test: ';
afterAll(() => {
fs.unlinkSync(outputFilePath);
});
it('retries failed tests', () => {
const result = runJest('test-retries', ['e2e.test.js']);
expect(result.exitCode).toEqual(0);
expect(result.failed).toBe(false);
expect(result.stdout.includes(logErrorsBeforeRetryErrorMessage)).toBe(
false,
);
});
it('logs error(s) before retry', () => {
const result = runJest('test-retries', ['logErrorsBeforeRetries.test.js']);
expect(result.exitCode).toEqual(0);
expect(result.failed).toBe(false);
expect(result.stdout.includes(logErrorsBeforeRetryErrorMessage)).toBe(true);
});
it('reporter shows more than 1 invocation if test is retried', () => {
let jsonResult;
const reporterConfig = {
reporters: [
['<rootDir>/reporters/RetryReporter.js', {output: outputFilePath}],
],
};
runJest('test-retries', [
'--config',
JSON.stringify(reporterConfig),
'retry.test.js',
]);
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.numPassedTests).toBe(0);
expect(jsonResult.numFailedTests).toBe(1);
expect(jsonResult.numPendingTests).toBe(0);
expect(jsonResult.testResults[0].testResults[0].invocations).toBe(4);
});
it('reporter shows 1 invocation if tests are not retried', () => {
let jsonResult;
const reporterConfig = {
reporters: [
['<rootDir>/reporters/RetryReporter.js', {output: outputFilePath}],
],
};
runJest('test-retries', [
'--config',
JSON.stringify(reporterConfig),
'control.test.js',
]);
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.numPassedTests).toBe(0);
expect(jsonResult.numFailedTests).toBe(1);
expect(jsonResult.numPendingTests).toBe(0);
expect(jsonResult.testResults[0].testResults[0].invocations).toBe(1);
});
it('tests are not retried if beforeAll hook failure occurs', () => {
let jsonResult;
const reporterConfig = {
reporters: [
['<rootDir>/reporters/RetryReporter.js', {output: outputFilePath}],
],
};
runJest('test-retries', [
'--config',
JSON.stringify(reporterConfig),
'beforeAllFailure.test.js',
]);
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.numPassedTests).toBe(0);
expect(jsonResult.numFailedTests).toBe(1);
expect(jsonResult.numPendingTests).toBe(0);
expect(jsonResult.testResults[0].testResults[0].invocations).toBe(1);
});
});