-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
failures.test.ts
208 lines (161 loc) · 5.6 KB
/
failures.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* 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 {wrap} from 'jest-snapshot-serializer-raw';
import {extractSummary, runYarn} from '../Utils';
import runJest from '../runJest';
const dir = path.resolve(__dirname, '../failures');
const normalizeDots = (text: string) => text.replace(/\.{1,}$/gm, '.');
function cleanStderr(stderr: string) {
const {rest} = extractSummary(stderr);
return rest
.replace(/.*(jest-jasmine2|jest-circus).*\n/g, '')
.replace(new RegExp('Failed: Object {', 'g'), 'thrown: Object {');
}
const nodeMajorVersion = Number(process.versions.node.split('.')[0]);
beforeAll(() => {
runYarn(dir);
});
test('not throwing Error objects', () => {
let stderr;
stderr = runJest(dir, ['throwNumber.test.js']).stderr;
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
stderr = runJest(dir, ['throwString.test.js']).stderr;
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
stderr = runJest(dir, ['throwObject.test.js']).stderr;
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
stderr = runJest(dir, ['assertionCount.test.js']).stderr;
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
stderr = runJest(dir, ['duringTests.test.js']).stderr;
if (nodeMajorVersion < 12) {
const lineEntry = '(__tests__/duringTests.test.js:38:8)';
expect(stderr).toContain(`at Object.<anonymous>.done ${lineEntry}`);
stderr = stderr.replace(
`at Object.<anonymous>.done ${lineEntry}`,
`at Object.<anonymous> ${lineEntry}`,
);
}
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
});
test('works with node assert', () => {
const {stderr} = runJest(dir, ['assertionError.test.js']);
let summary = normalizeDots(cleanStderr(stderr));
// Node 9 started to include the error for `doesNotThrow`
// https://github.com/nodejs/node/pull/12167
if (nodeMajorVersion >= 10) {
expect(summary).toContain(`
assert.doesNotThrow(function)
Expected the function not to throw an error.
Instead, it threw:
[Error: err!]
Message:
Got unwanted exception.
`);
expect(summary).toContain(`
68 |
69 | test('assert.doesNotThrow', () => {
> 70 | assert.doesNotThrow(() => {
| ^
71 | throw Error('err!');
72 | });
73 | });
at Object.doesNotThrow (__tests__/assertionError.test.js:70:10)
`);
const commonErrorMessage = `Message:
Got unwanted exception.
`;
if (nodeMajorVersion === 9) {
const specificErrorMessage = `Message:
Got unwanted exception.
err!
`;
expect(summary).toContain(specificErrorMessage);
summary = summary.replace(specificErrorMessage, commonErrorMessage);
} else {
const specificErrorMessage = `Message:
Got unwanted exception.
Actual message: "err!"
`;
expect(summary).toContain(specificErrorMessage);
summary = summary.replace(specificErrorMessage, commonErrorMessage);
}
const ifErrorMessage = `
assert.ifError(received, expected)
Expected value ifError to:
null
Received:
1
Message:
ifError got unwanted exception: 1
Difference:
Comparing two different types of values. Expected null but received number.
64 |
65 | test('assert.ifError', () => {
> 66 | assert.ifError(1);
| ^
67 | });
68 |
69 | test('assert.doesNotThrow', () => {
at Object.ifError (__tests__/assertionError.test.js:66:10)
`;
expect(summary).toContain(ifErrorMessage);
summary = summary.replace(ifErrorMessage, '');
} else {
const ifErrorMessage = `
thrown: 1
63 | });
64 |
> 65 | test('assert.ifError', () => {
| ^
66 | assert.ifError(1);
67 | });
68 |
at Object.test (__tests__/assertionError.test.js:65:1)
`;
expect(summary).toContain(ifErrorMessage);
summary = summary.replace(ifErrorMessage, '');
}
expect(wrap(summary)).toMatchSnapshot();
});
test('works with assertions in separate files', () => {
const {stderr} = runJest(dir, ['testMacro.test.js']);
expect(wrap(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.indexOf('packages/expect/build/index.js') === -1)
.join('\n');
// Remove replacements when jasmine is gone
const result = normalizeDots(rest)
.replace(/.*thrown:.*\n/, '')
.replace(/.*Use jest\.setTimeout\(newTimeout\).*/, '<REPLACED>')
.replace(/.*Timeout - Async callback was not.*/, '<REPLACED>');
expect(wrap(result)).toMatchSnapshot();
});
test('works with snapshot failures', () => {
const {stderr} = runJest(dir, ['snapshot.test.js']);
const result = normalizeDots(cleanStderr(stderr));
expect(
wrap(result.substring(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(
wrap(result.substring(0, result.indexOf('Snapshot Summary'))),
).toMatchSnapshot();
});
test('errors after test has completed', () => {
const {stderr} = runJest(dir, ['errorAfterTestComplete.test.js']);
expect(stderr).toMatch(
/Error: Caught error after test environment was torn down/,
);
expect(stderr).toMatch(/Failed: "fail async"/);
});