/
inline_snapshots.test.ts
201 lines (173 loc) · 5.19 KB
/
inline_snapshots.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
/**
* 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.
*
*/
jest.mock('fs');
jest.mock('prettier');
import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
import babelTraverse from '@babel/traverse';
import {saveInlineSnapshots} from '../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);
(fs.statSync as jest.Mock).mockImplementation(filePath => ({
isDirectory: () => !filePath.endsWith('.js'),
}));
fs.readdirSync = jest.fn(() => []);
(prettier.resolveConfig.sync as jest.Mock).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 as jest.Mock).mockImplementation(
() => `expect(1).toMatchInlineSnapshot();\n`,
);
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `1`,
},
],
prettier,
babelTraverse,
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
'expect(1).toMatchInlineSnapshot(`1`);\n',
);
});
test.each([['babylon'], ['flow'], ['typescript']])(
'saveInlineSnapshots() replaces existing template literal - %s parser',
parser => {
const filename = path.join(__dirname, 'my.test.js');
(fs.readFileSync as jest.Mock).mockImplementation(
() => 'expect(1).toMatchInlineSnapshot(`2`);\n',
);
(prettier.resolveConfig.sync as jest.Mock).mockReturnValue({parser});
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `1`,
},
],
prettier,
babelTraverse,
);
expect(
(prettier.resolveConfig.sync as jest.Mock).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 as jest.Mock).mockImplementation(
() => 'expect(1).toMatchInlineSnapshot({}, `2`);\n',
);
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 as jest.Mock).mockImplementation(
() => 'expect(1).toMatchInlineSnapshot();\n',
);
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 as jest.Mock).mockImplementation(
() => 'expect(1).toMatchInlineSnapshot();\n',
);
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 as jest.Mock).mockImplementation(
() => 'expect("`").toMatchInlineSnapshot();\n',
);
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 as jest.Mock).mockImplementation(
() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
);
(prettier.resolveConfig.sync as jest.Mock).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",
);
});