-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
snapshot.test.ts
332 lines (283 loc) · 10.8 KB
/
snapshot.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* 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 * as fs from 'graceful-fs';
import {wrap} from 'jest-snapshot-serializer-raw';
import {extractSummary} from '../Utils';
import runJest, {json as runWithJson} from '../runJest';
const emptyTest = 'describe("", () => {it("", () => {})})';
const snapshotDir = path.resolve(
__dirname,
'../snapshot/__tests__/__snapshots__',
);
const snapshotFile = path.resolve(snapshotDir, 'snapshot.test.js.snap');
const secondSnapshotFile = path.resolve(
snapshotDir,
'secondSnapshot.test.js.snap',
);
const snapshotOfCopy = path.resolve(snapshotDir, 'snapshot.test_copy.js.snap');
const originalTestPath = path.resolve(
__dirname,
'../snapshot/__tests__/snapshot.test.js',
);
const originalTestContent = fs.readFileSync(originalTestPath, 'utf8');
const copyOfTestPath = originalTestPath.replace(/\.js$/, '_copy.js');
const snapshotEscapeDir = path.resolve(
__dirname,
'../snapshot-escape/__tests__/',
);
const snapshotEscapeTestFile = path.resolve(
snapshotEscapeDir,
'snapshot.test.js',
);
const snapshotEscapeSnapshotDir = path.resolve(
snapshotEscapeDir,
'__snapshots__',
);
const snapshotEscapeFile = path.resolve(
snapshotEscapeSnapshotDir,
'snapshot.test.js.snap',
);
const snapshotEscapeRegexFile = path.resolve(
snapshotEscapeSnapshotDir,
'snapshotEscapeRegex.js.snap',
);
const snapshotEscapeSubstitutionFile = path.resolve(
snapshotEscapeSnapshotDir,
'snapshotEscapeSubstitution.test.js.snap',
);
const initialTestData = fs.readFileSync(snapshotEscapeTestFile, 'utf8');
const fileExists = (filePath: string) => {
try {
return fs.statSync(filePath).isFile();
} catch {}
return false;
};
const getSnapshotOfCopy = () => {
const exports = Object.create(null);
// eslint-disable-next-line no-eval
eval(fs.readFileSync(snapshotOfCopy, 'utf8'));
return exports;
};
describe('Snapshot', () => {
const cleanup = () => {
[
snapshotFile,
secondSnapshotFile,
snapshotOfCopy,
copyOfTestPath,
snapshotEscapeFile,
snapshotEscapeRegexFile,
snapshotEscapeSubstitutionFile,
].forEach(file => {
if (fileExists(file)) {
fs.unlinkSync(file);
}
});
if (fileExists(snapshotDir)) {
fs.rmdirSync(snapshotDir);
}
if (fileExists(snapshotEscapeSnapshotDir)) {
fs.rmdirSync(snapshotEscapeSnapshotDir);
}
fs.writeFileSync(snapshotEscapeTestFile, initialTestData, 'utf8');
};
beforeEach(cleanup);
afterAll(cleanup);
it('stores new snapshots on the first run', () => {
const {exitCode, json, stderr} = runWithJson('snapshot', [
'-w=1',
'--ci=false',
]);
expect(json.numTotalTests).toBe(5);
expect(json.numPassedTests).toBe(5);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(0);
expect(exitCode).toBe(0);
const content = require(snapshotFile);
expect(
content['snapshot is not influenced by previous counter 1'],
).not.toBeUndefined();
expect(stderr).toMatch('5 snapshots written from 2 test suites');
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
});
it('works with escaped characters', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot.test.js',
]);
let stderr = result.stderr;
expect(stderr).toMatch('1 snapshot written');
expect(result.exitCode).toBe(0);
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
// Write the second snapshot
const testData =
`test('escape strings two', () => expect('two: \\\'\"').` +
`toMatchSnapshot());`;
const newTestData = initialTestData + testData;
fs.writeFileSync(snapshotEscapeTestFile, newTestData, 'utf8');
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'--updateSnapshot',
'snapshot.test.js',
]);
stderr = result.stderr;
expect(stderr).toMatch('1 snapshot written');
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
expect(result.exitCode).toBe(0);
// Now let's check again if everything still passes.
// If this test doesn't pass, some snapshot data was not properly escaped.
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot.test.js',
]);
stderr = result.stderr;
expect(stderr).not.toMatch('Snapshot Summary');
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
expect(result.exitCode).toBe(0);
});
it('works with escaped regex', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshotEscapeRegex.js',
]);
let stderr = result.stderr;
expect(stderr).toMatch('2 snapshots written from 1 test suite.');
expect(result.exitCode).toBe(0);
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshotEscapeRegex.js',
]);
stderr = result.stderr;
// Make sure we aren't writing a snapshot this time which would
// indicate that the snapshot couldn't be loaded properly.
expect(stderr).not.toMatch('Snapshot Summary');
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
expect(result.exitCode).toBe(0);
});
it('works with template literal substitutions', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshotEscapeSubstitution.test.js',
]);
let stderr = result.stderr;
expect(stderr).toMatch('1 snapshot written');
expect(result.exitCode).toBe(0);
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshotEscapeSubstitution.test.js',
]);
stderr = result.stderr;
// Make sure we aren't writing a snapshot this time which would
// indicate that the snapshot couldn't be loaded properly.
expect(stderr).not.toMatch('1 snapshot written');
expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
expect(result.exitCode).toBe(0);
});
describe('Validation', () => {
beforeEach(() => {
fs.writeFileSync(copyOfTestPath, originalTestContent);
});
it('does not save snapshots in CI mode by default', () => {
const result = runWithJson('snapshot', ['-w=1', '--ci=true']);
expect(result.json.success).toBe(false);
expect(result.json.numTotalTests).toBe(9);
expect(result.json.snapshot.added).toBe(0);
expect(result.json.snapshot.total).toBe(9);
const {rest, summary} = extractSummary(result.stderr);
expect(rest).toMatch('New snapshot was not written');
expect(wrap(summary)).toMatchSnapshot();
});
it('works on subsequent runs without `-u`', () => {
const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);
const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
const secondRun = runWithJson('snapshot', []);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(9);
expect(secondRun.json.success).toBe(true);
expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
expect(secondRun.stderr).toMatch('9 passed, 9 total');
expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
});
it('deletes the snapshot if the test suite has been removed', () => {
const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);
fs.unlinkSync(copyOfTestPath);
const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
const secondRun = runWithJson('snapshot', ['-w=1', '--ci=false', '-u']);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(5);
expect(fileExists(snapshotOfCopy)).toBe(false);
expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
expect(secondRun.stderr).toMatch(
'1 snapshot file removed from 1 test suite',
);
expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
});
it('deletes a snapshot when a test does removes all the snapshots', () => {
const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);
fs.writeFileSync(copyOfTestPath, emptyTest);
const secondRun = runWithJson('snapshot', ['-w=1', '--ci=false', '-u']);
fs.unlinkSync(copyOfTestPath);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(6);
expect(fileExists(snapshotOfCopy)).toBe(false);
expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
expect(secondRun.stderr).toMatch(
'1 snapshot file removed from 1 test suite',
);
expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
});
it('updates the snapshot when a test removes some snapshots', () => {
const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);
fs.unlinkSync(copyOfTestPath);
const beforeRemovingSnapshot = getSnapshotOfCopy();
fs.writeFileSync(
copyOfTestPath,
originalTestContent.replace(
'.toMatchSnapshot()',
'.not.toBe(undefined)',
),
);
const secondRun = runWithJson('snapshot', ['-w=1', '--ci=false', '-u']);
fs.unlinkSync(copyOfTestPath);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(9);
expect(fileExists(snapshotOfCopy)).toBe(true);
const afterRemovingSnapshot = getSnapshotOfCopy();
expect(Object.keys(beforeRemovingSnapshot).length - 1).toBe(
Object.keys(afterRemovingSnapshot).length,
);
const keyToCheck =
'snapshot works with plain objects and the ' +
'title has `escape` characters 2';
expect(beforeRemovingSnapshot[keyToCheck]).not.toBe(undefined);
expect(afterRemovingSnapshot[keyToCheck]).toBe(undefined);
expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
expect(secondRun.stderr).toMatch('1 snapshot updated from 1 test suite');
expect(secondRun.stderr).toMatch('1 snapshot removed from 1 test suite');
});
});
});