forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailures.test.js
More file actions
127 lines (102 loc) · 3.43 KB
/
failures.test.js
File metadata and controls
127 lines (102 loc) · 3.43 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
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
const path = require('path');
const skipOnWindows = require('../../scripts/skip_on_windows');
const {extractSummary} = require('../utils');
const runJest = require('../runJest');
const dir = path.resolve(__dirname, '../failures');
const normalizeDots = text => text.replace(/\.{1,}$/gm, '.');
skipOnWindows.suite();
const cleanupStackTrace = stderr => {
const STACK_REGEXP = /^.*at.*(setup-jest-globals|extractExpectedAssertionsErrors).*\n/gm;
if (!STACK_REGEXP.test(stderr)) {
throw new Error(
`
This function is used to remove inconsistent stack traces between
jest-jasmine2 and jest-circus. If you see this error, that means the
stack traces are no longer inconsistent and this function can be
safely removed.
output:
${stderr}
`,
);
}
return (
stderr
.replace(STACK_REGEXP, '')
// Also remove trailing whitespace.
.replace(/\s+$/gm, '')
);
};
test('not throwing Error objects', () => {
let stderr;
stderr = runJest(dir, ['throw_number.test.js']).stderr;
expect(extractSummary(stderr).rest).toMatchSnapshot();
stderr = runJest(dir, ['throw_string.test.js']).stderr;
expect(extractSummary(stderr).rest).toMatchSnapshot();
stderr = runJest(dir, ['throw_object.test.js']).stderr;
expect(extractSummary(stderr).rest).toMatchSnapshot();
stderr = runJest(dir, ['assertion_count.test.js']).stderr;
expect(extractSummary(cleanupStackTrace(stderr)).rest).toMatchSnapshot();
});
test('works with node assert', () => {
const {stderr} = runJest(dir, ['node_assertion_error.test.js']);
let summary = normalizeDots(extractSummary(stderr).rest);
// Node 9 started to include the error for `doesNotThrow`
// https://github.com/nodejs/node/pull/12167
if (Number(process.versions.node.split('.')[0]) >= 9) {
expect(summary).toContain(`
assert.doesNotThrow(function)
Expected the function not to throw an error.
Instead, it threw:
[Error: err!]
Message:
Got unwanted exception.
err!
err!
69 |
70 | test('assert.doesNotThrow', () => {
> 71 | assert.doesNotThrow(() => {
72 | throw Error('err!');
73 | });
74 | });
at __tests__/node_assertion_error.test.js:71:10
`);
summary = summary.replace(
`Message:
Got unwanted exception.
err!
err!
`,
`Message:
Got unwanted exception.
`,
);
}
expect(summary).toMatchSnapshot();
});
test('works with assertions in separate files', () => {
const {stderr} = runJest(dir, ['test_macro.test.js']);
expect(normalizeDots(extractSummary(stderr).rest)).toMatchSnapshot();
});
test('works with async failures', () => {
const {stderr} = runJest(dir, ['async_failures.test.js']);
expect(normalizeDots(extractSummary(stderr).rest)).toMatchSnapshot();
});
test('works with snapshot failures', () => {
const {stderr} = runJest(dir, ['snapshot.test.js']);
const result = normalizeDots(extractSummary(stderr).rest);
expect(
result.substring(0, result.indexOf('Snapshot Summary')),
).toMatchSnapshot();
});
test('works with custom matchers', () => {
const {stderr} = runJest(dir, ['custom_matcher.test.js']);
expect(normalizeDots(extractSummary(stderr).rest)).toMatchSnapshot();
});