forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline_snapshots.test.js
More file actions
202 lines (174 loc) · 5.08 KB
/
inline_snapshots.test.js
File metadata and controls
202 lines (174 loc) · 5.08 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
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
/**
* 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
*/
jest.mock('fs');
jest.mock('prettier');
const fs = require('fs');
const path = require('path');
const prettier = require('prettier');
const babelTraverse = require('babel-traverse').default;
const {saveInlineSnapshots} = require('../inline_snapshots');
const writeFileSync = fs.writeFileSync;
const readFileSync = fs.readFileSync;
const existsSync = fs.existsSync;
const statSync = fs.statSync;
const readdirSync = fs.readdirSync;
beforeEach(() => {
fs.writeFileSync = jest.fn();
fs.readFileSync = jest.fn();
fs.existsSync = jest.fn(() => true);
// $FlowFixMe mock
fs.statSync = jest.fn(filePath => ({
isDirectory: () => !filePath.endsWith('.js'),
}));
fs.readdirSync = jest.fn(() => []);
prettier.resolveConfig.sync.mockReset();
});
afterEach(() => {
fs.writeFileSync = writeFileSync;
fs.readFileSync = readFileSync;
fs.existsSync = existsSync;
fs.statSync = statSync;
fs.readdirSync = readdirSync;
});
test('saveInlineSnapshots() replaces empty function call with a template literal', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => `expect(1).toMatchInlineSnapshot();\n`,
): any);
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `1`,
},
],
prettier,
babelTraverse,
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
'expect(1).toMatchInlineSnapshot(`1`);\n',
);
});
// $FlowFixMe test.each is not in flow-typed yet
test.each([['babylon'], ['flow'], ['typescript']])(
'saveInlineSnapshots() replaces existing template literal - %s parser',
parser => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot(`2`);\n',
): any);
prettier.resolveConfig.sync.mockReturnValue({parser});
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `1`,
},
],
prettier,
babelTraverse,
);
expect(prettier.resolveConfig.sync.mock.results[0].value).toEqual({parser});
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
'expect(1).toMatchInlineSnapshot(`1`);\n',
);
},
);
test('saveInlineSnapshots() replaces existing template literal with property matchers', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot({}, `2`);\n',
): any);
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `1`,
},
],
prettier,
babelTraverse,
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
'expect(1).toMatchInlineSnapshot({}, `1`);\n',
);
});
test('saveInlineSnapshots() throws if frame does not match', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot();\n',
): any);
const save = () =>
saveInlineSnapshots(
[
{
frame: {column: 2 /* incorrect */, file: filename, line: 1},
snapshot: `1`,
},
],
prettier,
babelTraverse,
);
expect(save).toThrowError(/Couldn't locate all inline snapshots./);
});
test('saveInlineSnapshots() throws if multiple calls to to the same location', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot();\n',
): any);
const frame = {column: 11, file: filename, line: 1};
const save = () =>
saveInlineSnapshots(
[{frame, snapshot: `1`}, {frame, snapshot: `2`}],
prettier,
babelTraverse,
);
expect(save).toThrowError(
/Multiple inline snapshots for the same call are not supported./,
);
});
test('saveInlineSnapshots() uses escaped backticks', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => 'expect("`").toMatchInlineSnapshot();\n',
): any);
const frame = {column: 13, file: filename, line: 1};
saveInlineSnapshots([{frame, snapshot: '`'}], prettier, babelTraverse);
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
'expect("`").toMatchInlineSnapshot(`\\``);\n',
);
});
test('saveInlineSnapshots() works with non-literals in expect call', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
): any);
prettier.resolveConfig.sync.mockReturnValue({
bracketSpacing: false,
singleQuote: true,
});
saveInlineSnapshots(
[
{
frame: {column: 18, file: filename, line: 1},
snapshot: `{a: 'a'}`,
},
],
prettier,
babelTraverse,
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
"expect({a: 'a'}).toMatchInlineSnapshot(`{a: 'a'}`);\n",
);
});