forked from jestjs/jest
/
utils.test.ts
460 lines (399 loc) · 13.6 KB
/
utils.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* 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('graceful-fs', () => ({
...jest.createMockFromModule<typeof import('fs')>('fs'),
existsSync: jest.fn().mockReturnValue(true),
}));
import * as path from 'path';
import assert = require('assert');
import * as fs from 'graceful-fs';
import chalk = require('chalk');
import {
SNAPSHOT_GUIDE_LINK,
SNAPSHOT_VERSION,
SNAPSHOT_VERSION_WARNING,
addExtraLineBreaks,
deepMerge,
getSnapshotData,
keyToTestName,
removeExtraLineBreaks,
removeLinesBeforeExternalMatcherTrap,
saveSnapshotFile,
serialize,
testNameToKey,
} from '../utils';
test('keyToTestName()', () => {
expect(keyToTestName('abc cde 12')).toBe('abc cde');
expect(keyToTestName('abc cde 12')).toBe('abc cde ');
expect(() => keyToTestName('abc cde')).toThrowError(
'Snapshot keys must end with a number.',
);
});
test('testNameToKey', () => {
expect(testNameToKey('abc cde', 1)).toBe('abc cde 1');
expect(testNameToKey('abc cde ', 12)).toBe('abc cde 12');
});
test('saveSnapshotFile() works with \r\n', () => {
const filename = path.join(__dirname, 'remove-newlines.snap');
const data = {
myKey: '<div>\r\n</div>',
};
saveSnapshotFile(data, filename);
expect(fs.writeFileSync).toBeCalledWith(
filename,
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n',
);
});
test('saveSnapshotFile() works with \r', () => {
const filename = path.join(__dirname, 'remove-newlines.snap');
const data = {
myKey: '<div>\r</div>',
};
saveSnapshotFile(data, filename);
expect(fs.writeFileSync).toBeCalledWith(
filename,
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n',
);
});
test('getSnapshotData() throws when no snapshot version', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
(fs.readFileSync as jest.Mock).mockImplementation(
() => 'exports[`myKey`] = `<div>\n</div>`;\n',
);
const update = 'none';
expect(() => getSnapshotData(filename, update)).toThrowError(
chalk.red(
`${chalk.bold('Outdated snapshot')}: No snapshot header found. ` +
`Jest 19 introduced versioned snapshots to ensure all developers on ` +
`a project are using the same version of Jest. ` +
`Please update all snapshots during this upgrade of Jest.\n\n`,
) + SNAPSHOT_VERSION_WARNING,
);
});
test('getSnapshotData() throws for older snapshot version', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
(fs.readFileSync as jest.Mock).mockImplementation(
() =>
`// Jest Snapshot v0.99, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n',
);
const update = 'none';
expect(() => getSnapshotData(filename, update)).toThrowError(
chalk.red(
`${chalk.red.bold('Outdated snapshot')}: The version of the snapshot ` +
`file associated with this test is outdated. The snapshot file ` +
`version ensures that all developers on a project are using ` +
`the same version of Jest. ` +
`Please update all snapshots during this upgrade of Jest.\n\n`,
) +
`Expected: v${SNAPSHOT_VERSION}\n` +
`Received: v0.99\n\n` +
SNAPSHOT_VERSION_WARNING,
);
});
test('getSnapshotData() throws for newer snapshot version', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
(fs.readFileSync as jest.Mock).mockImplementation(
() =>
`// Jest Snapshot v2, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n',
);
const update = 'none';
expect(() => getSnapshotData(filename, update)).toThrowError(
chalk.red(
`${chalk.red.bold('Outdated Jest version')}: The version of this ` +
`snapshot file indicates that this project is meant to be used ` +
`with a newer version of Jest. ` +
`The snapshot file version ensures that all developers on a project ` +
`are using the same version of Jest. ` +
`Please update your version of Jest and re-run the tests.\n\n`,
) +
`Expected: v${SNAPSHOT_VERSION}\n` +
`Received: v2`,
);
});
test('getSnapshotData() does not throw for when updating', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
(fs.readFileSync as jest.Mock).mockImplementation(
() => 'exports[`myKey`] = `<div>\n</div>`;\n',
);
const update = 'all';
expect(() => getSnapshotData(filename, update)).not.toThrow();
});
test('getSnapshotData() marks invalid snapshot dirty when updating', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
(fs.readFileSync as jest.Mock).mockImplementation(
() => 'exports[`myKey`] = `<div>\n</div>`;\n',
);
const update = 'all';
expect(getSnapshotData(filename, update)).toMatchObject({dirty: true});
});
test('getSnapshotData() marks valid snapshot not dirty when updating', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
(fs.readFileSync as jest.Mock).mockImplementation(
() =>
`// Jest Snapshot v${SNAPSHOT_VERSION}, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n',
);
const update = 'all';
expect(getSnapshotData(filename, update)).toMatchObject({dirty: false});
});
test('escaping', () => {
const filename = path.join(__dirname, 'escaping.snap');
const data = '"\'\\';
const writeFileSync = fs.writeFileSync as jest.Mock;
writeFileSync.mockReset();
saveSnapshotFile({key: data}, filename);
const writtenData = writeFileSync.mock.calls[0][1];
expect(writtenData).toBe(
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`key`] = `"\'\\\\`;\n',
);
// @ts-expect-error
const exports = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line no-eval
const readData = eval('var exports = {}; ' + writtenData + ' exports');
expect(readData).toEqual({key: data});
const snapshotData = readData.key;
expect(data).toEqual(snapshotData);
});
test('serialize handles \\r\\n', () => {
const data = '<div>\r\n</div>';
const serializedData = serialize(data);
expect(serializedData).toBe('"<div>\n</div>"');
});
describe('ExtraLineBreaks', () => {
test('0 empty string', () => {
const expected = '';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe(expected);
expect(removed).toBe(expected);
});
test('1 line has double quote marks at edges', () => {
const expected = '" one line "';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe(expected);
expect(removed).toBe(expected);
});
test('1 line has spaces at edges', () => {
const expected = ' one line ';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe(expected);
expect(removed).toBe(expected);
});
test('2 lines both are blank', () => {
const expected = '\n';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe('\n' + expected + '\n');
expect(removed).toBe(expected);
});
test('2 lines have double quote marks at edges', () => {
const expected = '"\n"';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe('\n' + expected + '\n');
expect(removed).toBe(expected);
});
test('2 lines first is blank', () => {
const expected = '\nsecond line ';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe('\n' + expected + '\n');
expect(removed).toBe(expected);
});
test('2 lines last is blank', () => {
const expected = ' first line\n';
const added = addExtraLineBreaks(expected);
const removed = removeExtraLineBreaks(added);
expect(added).toBe('\n' + expected + '\n');
expect(removed).toBe(expected);
});
});
describe('removeLinesBeforeExternalMatcherTrap', () => {
test('contains external matcher trap', () => {
const stack = `Error:
at SnapshotState._addSnapshot (/jest/packages/jest-snapshot/build/State.js:150:9)
at SnapshotState.match (/jest/packages/jest-snapshot/build/State.js:303:14)
at _toMatchSnapshot (/jest/packages/jest-snapshot/build/index.js:399:32)
at _toThrowErrorMatchingSnapshot (/jest/packages/jest-snapshot/build/index.js:585:10)
at Object.toThrowErrorMatchingInlineSnapshot (/jest/packages/jest-snapshot/build/index.js:504:10)
at Object.<anonymous> (/jest/packages/expect/build/index.js:138:20)
at __EXTERNAL_MATCHER_TRAP__ (/jest/packages/expect/build/index.js:378:30)
at throwingMatcher (/jest/packages/expect/build/index.js:379:15)
at /jest/packages/expect/build/index.js:285:72
at Object.<anonymous> (/jest/e2e/to-throw-error-matching-inline-snapshot/__tests__/should-support-rejecting-promises.test.js:3:7)`;
const expected = ` at throwingMatcher (/jest/packages/expect/build/index.js:379:15)
at /jest/packages/expect/build/index.js:285:72
at Object.<anonymous> (/jest/e2e/to-throw-error-matching-inline-snapshot/__tests__/should-support-rejecting-promises.test.js:3:7)`;
expect(removeLinesBeforeExternalMatcherTrap(stack)).toBe(expected);
});
test("doesn't contain external matcher trap", () => {
const stack = `Error:
at SnapshotState._addSnapshot (/jest/packages/jest-snapshot/build/State.js:150:9)
at SnapshotState.match (/jest/packages/jest-snapshot/build/State.js:303:14)
at _toMatchSnapshot (/jest/packages/jest-snapshot/build/index.js:399:32)
at _toThrowErrorMatchingSnapshot (/jest/packages/jest-snapshot/build/index.js:585:10)
at Object.toThrowErrorMatchingInlineSnapshot (/jest/packages/jest-snapshot/build/index.js:504:10)
at Object.<anonymous> (/jest/packages/expect/build/index.js:138:20)
at throwingMatcher (/jest/packages/expect/build/index.js:379:15)
at /jest/packages/expect/build/index.js:285:72
at Object.<anonymous> (/jest/e2e/to-throw-error-matching-inline-snapshot/__tests__/should-support-rejecting-promises.test.js:3:7)`;
expect(removeLinesBeforeExternalMatcherTrap(stack)).toBe(stack);
});
});
describe('DeepMerge with property matchers', () => {
const matcher = expect.any(String);
/* eslint-disable sort-keys */
// to keep keys in numerical order rather than alphabetical
const cases = [
[
'a nested object',
// Target
{
data: {
one: 'one',
two: 'two',
},
},
// Matchers
{
data: {
two: matcher,
},
},
// Expected
{
data: {
one: 'one',
two: matcher,
},
},
],
[
'an object with an array of objects',
// Target
{
data: {
one: [
{
two: 'two',
three: 'three',
},
// Include an array element not present in the propertyMatchers
{
four: 'four',
five: 'five',
},
],
six: [{seven: 'seven'}],
nine: [[{ten: 'ten'}]],
},
},
// Matchers
{
data: {
one: [
{
two: matcher,
},
],
six: [
{seven: matcher},
// Include an array element not present in the target
{eight: matcher},
],
nine: [[{ten: matcher}]],
},
},
// Expected
{
data: {
one: [
{
two: matcher,
three: 'three',
},
{
four: 'four',
five: 'five',
},
],
six: [{seven: matcher}, {eight: matcher}],
nine: [[{ten: matcher}]],
},
},
],
[
'an object with an array of strings',
// Target
{
data: {
one: ['one'],
two: ['two'],
three: ['three', 'four'],
five: ['five'],
},
},
// Matchers
{
data: {
one: [matcher],
two: ['two'],
three: [matcher],
five: 'five',
},
},
// Expected
{
data: {
one: [matcher],
two: ['two'],
three: [matcher, 'four'],
five: 'five',
},
},
],
[
'an array of objects',
// Target
[{name: 'one'}, {name: 'two'}, {name: 'three'}],
// Matchers
[{name: 'one'}, {name: matcher}, {name: matcher}],
// Expected
[{name: 'one'}, {name: matcher}, {name: matcher}],
],
[
'an array of arrays',
// Target
[['one'], ['two'], ['three']],
// Matchers
[['one'], [matcher], [matcher]],
// Expected
[['one'], [matcher], [matcher]],
],
];
/* eslint-enable sort-keys */
it.each(cases)(
'Correctly merges %s',
(_case, target, propertyMatchers, expected) => {
const originalTarget = JSON.parse(JSON.stringify(target));
const mergedOutput = deepMerge(target, propertyMatchers);
// Use assert.deepStrictEqual() instead of expect().toStrictEqual()
// since we want to actually validate that we got the matcher
// rather than treat it specially the way that expect() does
assert.deepStrictEqual(mergedOutput, expected);
// Ensure original target is not modified
expect(target).toStrictEqual(originalTarget);
},
);
});