-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathfailures.test.ts
More file actions
138 lines (108 loc) · 4.68 KB
/
failures.test.ts
File metadata and controls
138 lines (108 loc) · 4.68 KB
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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 {extractSummary, runYarnInstall} from '../Utils';
import runJest, {json as runJestJson} from '../runJest';
const dir = path.resolve(__dirname, '../failures');
const normalizeDots = (text: string) => text.replaceAll(/\.+$/gm, '.');
function cleanStderr(stderr: string) {
const {rest} = extractSummary(stderr);
return rest
.replaceAll(/.*(jest-jasmine2|jest-circus).*\n/g, '')
.replaceAll(new RegExp('Failed: Object {', 'g'), 'thrown: Object {');
}
beforeAll(() => {
runYarnInstall(dir);
});
test('not throwing Error objects', () => {
let stderr;
stderr = runJest(dir, ['throwNumber.test.js']).stderr;
expect(cleanStderr(stderr)).toMatchSnapshot();
stderr = runJest(dir, ['throwString.test.js']).stderr;
expect(cleanStderr(stderr)).toMatchSnapshot();
stderr = runJest(dir, ['throwObject.test.js']).stderr;
expect(cleanStderr(stderr)).toMatchSnapshot();
stderr = runJest(dir, ['assertionCount.test.js']).stderr;
expect(cleanStderr(stderr)).toMatchSnapshot();
stderr = runJest(dir, ['duringTests.test.js']).stderr;
expect(cleanStderr(stderr)).toMatchSnapshot();
stderr = runJest(dir, ['throwObjectWithStackProp.test.js']).stderr;
expect(cleanStderr(stderr)).toMatchSnapshot();
});
test('works with node assert', () => {
const {stderr} = runJest(dir, ['assertionError.test.js']);
const summary = normalizeDots(cleanStderr(stderr));
expect(summary).toMatchSnapshot();
});
test('works with assertions in separate files', () => {
const {stderr} = runJest(dir, ['testMacro.test.js']);
expect(normalizeDots(cleanStderr(stderr))).toMatchSnapshot();
});
test('works with async failures', () => {
const {stderr} = runJest(dir, ['asyncFailures.test.js']);
const rest = cleanStderr(stderr)
.split('\n')
.filter(line => !line.includes('packages/expect/build/index.js'))
.join('\n');
// Remove replacements when jasmine is gone
const result = normalizeDots(rest)
.replace(/.*thrown:.*\n/, '')
.replace(
/.*Add a timeout value to this test to increase the timeout, if this is a long-running test. See https:\/\/jestjs.io\/docs\/api#testname-fn-timeout.+/,
'<REPLACED>',
)
.replace(/.*Timeout - Async callback was not.*/, '<REPLACED>');
expect(result).toMatchSnapshot();
});
test('works with snapshot failures', () => {
const {stderr} = runJest(dir, ['snapshot.test.js']);
const result = normalizeDots(cleanStderr(stderr));
expect(result.slice(0, result.indexOf('Snapshot Summary'))).toMatchSnapshot();
});
test('works with snapshot failures with hint', () => {
const {stderr} = runJest(dir, ['snapshotWithHint.test.js']);
const result = normalizeDots(cleanStderr(stderr));
expect(result.slice(0, result.indexOf('Snapshot Summary'))).toMatchSnapshot();
});
test('works with error with cause', () => {
const {stderr} = runJest(dir, ['errorWithCause.test.js']);
const summary = normalizeDots(cleanStderr(stderr));
expect(summary).toMatchSnapshot();
});
test('works with error with cause thrown outside tests', () => {
const {stderr} = runJest(dir, ['errorWithCauseInDescribe.test.js']);
const summary = normalizeDots(cleanStderr(stderr));
const sanitizedSummary = summary
.replaceAll(' Suite.f ', ' f ') // added by jasmine runner
.split('\n')
.map(line => line.trim()) // jasmine runner does not come with the same indentation
.join('\n');
expect(
// jasmine runner differ from circus one in this case, we just start
// the comparison when the stack starts to be reported
sanitizedSummary.slice(sanitizedSummary.indexOf('error during f')),
).toMatchSnapshot();
});
test('includes error causes in JSON failureMessages', () => {
// Stderr cause coverage is handled by snapshot tests above; this assertion
// targets the structured JSON payload consumed by reporters and integrations.
const {json} = runJestJson(dir, ['errorWithCause.test.js']);
const result = json.testResults[0];
const failureMessages =
result.assertionResults.flatMap(result => result.failureMessages) ?? [];
const failureOutput = failureMessages.join('\n');
expect(failureMessages).toHaveLength(3);
expect(failureOutput).toContain('[cause]: Error: error during g');
expect(failureOutput).toContain('[cause]: here is the cause');
});
test('errors after test has completed', () => {
const {stderr} = runJest(dir, ['errorAfterTestComplete.test.js']);
expect(stderr).toMatch(
/Error(WithStack)?: Caught error after test environment was torn down/,
);
expect(stderr).toMatch(/Failed: "fail async"/);
});