-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathtoThrowErrorMatchingSnapshot.test.ts
124 lines (107 loc) · 4.12 KB
/
toThrowErrorMatchingSnapshot.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
/**
* 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 * as fs from 'graceful-fs';
import {cleanup, makeTemplate, writeFiles} from '../Utils';
import runJest from '../runJest';
const DIR = path.resolve(__dirname, '../to-throw-error-matching-snapshot');
const TESTS_DIR = path.resolve(DIR, '__tests__');
beforeEach(() => cleanup(TESTS_DIR));
afterAll(() => cleanup(TESTS_DIR));
test('works fine when function throws error', () => {
const filename = 'works-fine-when-function-throws-error.test.js';
const template =
makeTemplate(`test('works fine when function throws error', () => {
expect(() => { throw new Error('apple'); })
.toThrowErrorMatchingSnapshot();
});
`);
{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(exitCode).toBe(0);
}
});
test("throws the error if tested function didn't throw error", () => {
const filename = 'throws-if-tested-function-did-not-throw.test.js';
const template =
makeTemplate(`test('throws the error if tested function did not throw error', () => {
expect(() => {}).toThrowErrorMatchingSnapshot();
});
`);
{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Received function did not throw');
expect(stderr).toMatch('Returned: undefined');
expect(exitCode).toBe(1);
}
});
test("reports stringified returned value if tested function didn't throw error", () => {
const filename = 'throws-if-tested-function-did-not-throw.test.js';
const template =
makeTemplate(`test('throws the error if tested function did not throw error', () => {
expect(() => { return { foo: 1, bar: { baz: "2", } };}).toThrowErrorMatchingSnapshot();
});
`);
{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Received function did not throw');
expect(stderr).toMatch('Returned: {"bar": {"baz": "2"}, "foo": 1}');
expect(exitCode).toBe(1);
}
});
test('accepts custom snapshot name', () => {
const filename = 'accept-custom-snapshot-name.test.js';
const template = makeTemplate(`test('accepts custom snapshot name', () => {
expect(() => { throw new Error('apple'); })
.toThrowErrorMatchingSnapshot('custom-name');
});
`);
{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(exitCode).toBe(0);
}
});
test('cannot be used with .not', () => {
const filename = 'cannot-be-used-with-not.test.js';
const template = makeTemplate(`test('cannot be used with .not', () => {
expect(() => { throw new Error('apple'); })
.not
.toThrowErrorMatchingSnapshot();
});
`);
{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Snapshot matchers cannot be used with not');
expect(exitCode).toBe(1);
}
});
test('should support rejecting promises', () => {
const filename = 'should-support-rejecting-promises.test.js';
const template =
makeTemplate(`test('should support rejecting promises', () => {
return expect(Promise.reject(new Error('octopus'))).rejects.toThrowErrorMatchingSnapshot();
});
`);
{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
const snapshot = fs.readFileSync(
`${TESTS_DIR}/__snapshots__/${filename}.snap`,
'utf8',
);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(snapshot).toMatchSnapshot();
expect(exitCode).toBe(0);
}
});