/
snapshot.spec.js
610 lines (509 loc) · 19.3 KB
/
snapshot.spec.js
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
'use strict';
import ClickCounter from '../components/ClickCounter';
import fs from 'fs';
import mockFs from 'mock-fs';
import Module from 'module';
import path from 'path';
import Promise from 'bluebird';
import React, { PropTypes } from 'react';
import ReactTestRenderer from 'react-test-renderer';
import Sinon from 'sinon';
import { injectLoader } from '../../helpers/snapshotLoader';
import Unexpected from 'unexpected';
import UnexpectedSinon from 'unexpected-sinon';
// Note: These are imported later than the others, so that jasmine is mocked for the jest-matchers, but
// unexpected does not think it's running under jasmine
import mockJasmine from '../helpers/mock-jasmine';
import JestMatchers from 'jest-matchers';
import UnexpectedReactTest from '../../test-renderer-jest'
import functionFixtures from '../fixtures/functions';
import { injectStateHooks } from '../../helpers/snapshots';
function loadSnapshotMock(snapshotPath) {
const snapModule = new Module(snapshotPath, null);
snapModule.load(snapshotPath);
return snapModule.exports;
}
injectLoader(loadSnapshotMock);
const expect = Unexpected.clone()
.use(UnexpectedReactTest)
.use(UnexpectedSinon);
expect.output.preferredWidth = 80;
Promise.promisifyAll(fs);
const fixtures = {};
describe('snapshots', function () {
const PATH_TO_TESTS = '/path/to/tests';
let state, removeUncheckedKeysStub;
before(function (done) {
fs.readdirAsync(path.join(__dirname, '../fixtures'))
.then(dirList => {
return Promise.all(dirList.map(entry => {
return fs.readFileAsync(path.join(__dirname, '../fixtures', entry))
.then(data => {
fixtures[path.basename(entry, '.snapshot')] = data.toString('utf-8');
});
}));
}).then(() => done())
.catch(e => done(e));
});
beforeEach(function () {
removeUncheckedKeysStub = Sinon.stub();
state = {
testPath: '/tmp/changeme.js',
currentTestName: 'foo',
snapshotState: {
added: 0,
updated: 0,
unmatched: 0,
update: undefined,
removeUncheckedKeys: removeUncheckedKeysStub
}
};
JestMatchers.setState(state);
Sinon.spy(fs, 'writeFileSync');
injectStateHooks();
});
afterEach(function () {
fs.writeFileSync.restore();
});
beforeEach(function () {
mockFs({
[PATH_TO_TESTS + '/__snapshots__/single.spec.unexpected-snap']: fixtures.single,
[PATH_TO_TESTS + '/__snapshots__/multiple.spec.unexpected-snap']: fixtures.multiple,
[PATH_TO_TESTS + '/__snapshots__/multipleclasses.spec.unexpected-snap']: fixtures.multipleclasses,
});
});
afterEach(function () {
mockFs.restore();
});
function initState(options) {
state.testPath = path.join(PATH_TO_TESTS, options.testPath);
state.currentTestName = options.testName;
state.unexpectedSnapshot = null;
if (options.update) {
state.snapshotState.update = options.update;
}
JestMatchers.setState(state);
}
it('passes a single test snapshot', function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(renderer, 'to match snapshot');
expect(fs.writeFileSync, 'was not called')
});
it('updates the `matched` count', function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(renderer, 'to match snapshot');
expect(state.snapshotState, 'to satisfy', {
matched: 1
});
});
it('passes multiple test snapshots', function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(renderer, 'to match snapshot');
expect(renderer, 'with event', 'click', 'to match snapshot');
expect(fs.writeFileSync, 'was not called')
});
describe('for an unknown test', function () {
let snapshotPath;
beforeEach(function () {
initState({
testPath: 'single.spec.js',
testName: 'a new test'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
snapshotPath = path.join(PATH_TO_TESTS, '__snapshots__/single.spec.unexpected-snap');
expect(renderer, 'to match snapshot');
});
it('writes a new snapshot', function () {
// Confirm we wrote the file with the old and new entries
expect(fs.writeFileSync, 'to have a call satisfying', [
snapshotPath,
expect.it('to match', /exports\[`a new test 1`]/)
.and('to match', /exports\[`single test name 1`]/)
]);
});
it('creates the correct snapshot', function () {
// Confirm it is parseable and contains the right thing
const newSnapshot = loadSnapshotMock(snapshotPath);
expect(newSnapshot['a new test 1'], 'to satisfy', {
type: 'button',
children: ['Clicked ', '0', ' times']
});
});
it('increments the added count in the state', function () {
expect(state, 'to satisfy', {
snapshotState: {
added: 1
}
});
});
});
it('fails on a snapshot that doesn`t match', function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(
() => expect(renderer, 'with event click', 'to match snapshot'),
'to throw',
[
'expected',
'<button onClick={function bound onClick() { /* native code */ }}>',
' Clicked 1 times',
'</button>',
"with event 'click' to match snapshot",
'',
'<button onClick={function bound onClick() { /* native code */ }}>',
' Clicked 1 times // -Clicked 1 times',
' // +Clicked 0 times',
'</button>'
].join('\n')
);
});
it('matches classes in the wrong order', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three two one" />);
expect(renderer, 'to match snapshot');
});
it('diffs missing classes', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three one" />);
expect(() => expect(renderer, 'to match snapshot'), 'to throw',
[
'expected',
'<button className="three one" onClick={function bound onClick() { /* native code */ }}>',
' Clicked 0 times',
'</button>',
'to match snapshot',
'',
'<button className="three one" // missing class \'two\'',
' onClick={function bound onClick() { /* native code */ }}>',
' Clicked 0 times',
'</button>'
].join('\n'))
});
it('diffs extra classes', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three two one four" />);
expect(() => expect(renderer, 'to match snapshot'), 'to throw',
[
'expected',
'<button className="three two one four"',
' onClick={function bound onClick() { /* native code */ }}>',
' Clicked 0 times',
'</button>',
'to match snapshot',
'',
'<button className="three two one four" // extra class \'four\'',
' onClick={function bound onClick() { /* native code */ }}>',
' Clicked 0 times',
'</button>'
].join('\n'))
});
it('diffs extra attributes', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three two one" ariaLabel="testextra" />);
expect(() => expect(renderer, 'to match snapshot'), 'to throw',
[
'expected',
'<button className="three two one"',
' onClick={function bound onClick() { /* native code */ }}',
' ariaLabel="testextra">',
' Clicked 0 times',
'</button>',
'to match snapshot',
'',
'<button className="three two one"',
' onClick={function bound onClick() { /* native code */ }}',
' ariaLabel="testextra" // ariaLabel should be removed',
'>',
' Clicked 0 times',
'</button>'
].join('\n'))
});
it('allows extra attributes with `to satisfy snapshot`', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three two one" ariaLabel="testextra" />);
expect(renderer, 'to satisfy snapshot');
});
it('allows extra classes with `to satisfy snapshot`', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three two one four" />);
expect(renderer, 'to satisfy snapshot');
});
it('allows extra classes with `to satisfy snapshot` after event ', function () {
initState({
testPath: 'multipleclasses.spec.js',
testName: 'multiple classes click'
});
const renderer = ReactTestRenderer.create(<ClickCounter className="three two one four" />);
expect(renderer, 'with event click', 'to satisfy snapshot');
});
it('increments `unmatched` when a snapshot doesn`t match', function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(
() => expect(renderer, 'with event click', 'to match snapshot'),
'to throw');
expect(state, 'to satisfy', {
snapshotState: {
unmatched: 1
}
});
});
describe('when update is true and the snapshot matches', function () {
beforeEach(function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name',
update: true
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(renderer, 'to match snapshot');
});
it('increments `matched`', function () {
expect(state, 'to satisfy', {
snapshotState: {
updated: 0,
added: 0,
matched: 1,
update: true
}
});
});
});
describe('when update is true and the snapshot doesn`t match', function () {
let snapshotPath;
beforeEach(function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name',
update: true
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
snapshotPath = path.join(PATH_TO_TESTS, '__snapshots__/single.spec.unexpected-snap');
expect(renderer, 'with event click', 'to match snapshot');
});
it('increments `updated`', function () {
expect(state, 'to satisfy', {
snapshotState: {
updated: 1,
added: 0,
update: true
}
});
});
it('writes the new snapshot', function () {
expect(fs.writeFileSync, 'to have calls satisfying', [
[
snapshotPath,
expect.it('to match', /exports\[`single test name 1`]/)
]
]);
});
it('writes the correct snapshot', function () {
const snapshot = loadSnapshotMock(snapshotPath);
expect(snapshot, 'to satisfy', {
'single test name 1': {
type: 'button',
children: [ 'Clicked ', '1', ' times' ]
}
});
});
});
describe('with functions', function () {
it('compares with a snapshot with a normal function', function () {
initState({
testPath: 'withFunctions.spec.js',
testName: 'with functions',
});
let renderer = ReactTestRenderer.create(<ClickCounter onMouseDown={functionFixtures.anonymous()} />);
expect(renderer, 'to match snapshot');
// Now reset state back such that it actually tests the snapshot
initState({
testPath: 'withFunctions.spec.js',
testName: 'with functions',
});
// Rerender, with a new instance of the anonymous function
renderer = ReactTestRenderer.create(<ClickCounter onMouseDown={functionFixtures.anonymous()} />);
expect(renderer, 'to match snapshot');
});
it('compares with a snapshot with a bound function', function () {
initState({
testPath: 'withFunctions.spec.js',
testName: 'with functions',
});
let renderer = ReactTestRenderer.create(<ClickCounter onMouseDown={functionFixtures.boundContentArgs()} />);
expect(renderer, 'to match snapshot');
// Now reset state back such that it actually tests the snapshot
initState({
testPath: 'withFunctions.spec.js',
testName: 'with functions',
});
// Rerender, with a new instance of the function
renderer = ReactTestRenderer.create(<ClickCounter onMouseDown={functionFixtures.boundContentArgs()} />);
expect(renderer, 'to match snapshot');
});
it('fails with a snapshot with a normal function when the expected is bound', function () {
initState({
testPath: 'withFunctions.spec.js',
testName: 'with functions',
});
let renderer = ReactTestRenderer.create(<ClickCounter onMouseDown={functionFixtures.boundContentArgs()} />);
// Create the snapshot with the bound function
expect(renderer, 'to match snapshot');
// Now reset state back such that it actually tests the snapshot
initState({
testPath: 'withFunctions.spec.js',
testName: 'with functions',
});
// Rerender, with a different unbound function
renderer = ReactTestRenderer.create(<ClickCounter onMouseDown={functionFixtures.namedContentArgs()} />);
expect(
() => expect(renderer, 'to match snapshot'),
'to throw',
[
'expected',
'<button onClick={function bound onClick() { /* native code */ }}',
' onMouseDown={function doStuff(a, b) { /* ... */ }}>',
' Clicked 0 times',
'</button>',
'to match snapshot',
'',
'<button onClick={function bound onClick() { /* native code */ }}',
' onMouseDown={function doStuff(a, b) { /* ... */ }} // expected',
' // function doStuff(a, b) {',
' // // comment',
' // return a + b;',
' // }',
' // to satisfy function bound bound3() { /* function body */ }',
' //',
' // -function doStuff(a, b) { /* function body */ }',
' // +function bound bound3() { /* function body */ }',
'>',
' Clicked 0 times',
'</button>'
].join('\n')
);
});
});
describe('removing unused keys', function () {
it('removes the unused snapshot file when removeUnusedKeys is called', function () {
initState({
testPath: 'single.spec.js',
testName: 'single test name',
update: true
});
Sinon.spy(fs, 'unlinkSync');
try {
// removeUncheckedKeys is called by Jest when update is true
state.snapshotState.removeUncheckedKeys();
expect(fs.unlinkSync, 'to have calls satisfying', [
[path.join(PATH_TO_TESTS, '__snapshots__/single.spec.unexpected-snap')]
]);
} finally {
fs.unlinkSync.restore();
}
});
it('removes the unused keys of a test where only some are used', function () {
initState({
testPath: 'multiple.spec.js',
testName: 'multi test two',
update: true
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(renderer, 'with event click', 'to match snapshot');
const originalSnapshot = loadSnapshotMock(path.join(PATH_TO_TESTS, '__snapshots__/multiple.spec.unexpected-snap'));
state.snapshotState.removeUncheckedKeys();
const newSnapshot = loadSnapshotMock(path.join(PATH_TO_TESTS, '__snapshots__/multiple.spec.unexpected-snap'));
expect(Object.keys(originalSnapshot), 'to equal', [ 'multi test one 1', 'multi test two 1', 'multi test two 2' ]);
expect(Object.keys(newSnapshot), 'to equal', [ 'multi test two 1']);
});
it('calls the original removeUncheckedKeys', function () {
initState({
testPath: 'multiple.spec.js',
testName: 'multi test two',
update: true
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(renderer, 'with event click', 'to match snapshot');
state.snapshotState.removeUncheckedKeys();
expect(removeUncheckedKeysStub, 'to have calls satisfying', [
{ args: [], 'this': state.snapshotState }
]);
});
});
it('leaves snapshots of tests that failed', function () {
initState({
testPath: 'multiple.spec.js',
testName: 'multi test two'
});
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(
() => expect(renderer, 'to match snapshot'),
'to throw');
const originalSnapshot = loadSnapshotMock(path.join(PATH_TO_TESTS, '__snapshots__/multiple.spec.unexpected-snap'));
state.snapshotState.removeUncheckedKeys();
const newSnapshot = loadSnapshotMock(path.join(PATH_TO_TESTS, '__snapshots__/multiple.spec.unexpected-snap'));
expect(Object.keys(originalSnapshot), 'to equal', [ 'multi test one 1', 'multi test two 1', 'multi test two 2' ]);
expect(Object.keys(newSnapshot), 'to equal', [ 'multi test two 1', 'multi test two 2' ]);
});
it('removes a file if there are no snapshots', function () {
initState({
testPath: 'multiple.spec.js',
testName: 'multi test two'
});
Sinon.spy(fs, 'unlinkSync');
try {
state.snapshotState.removeUncheckedKeys();
expect(fs.unlinkSync, 'to have calls satisfying', function () {
fs.unlinkSync(path.join(PATH_TO_TESTS, '__snapshots__/multiple.spec.unexpected-snap'));
});
} finally {
fs.unlinkSync.restore();
}
});
it('shows an error message if the JSON is asserted on directly', function () {
const renderer = ReactTestRenderer.create(<ClickCounter />);
expect(() => expect(renderer.toJSON(), 'to match snapshot'), 'to throw',
[
'To assert snapshots, use the testRenderer directly, not the result of `.toJSON()`',
'e.g.',
' const testRenderer = ReactTestRenderer.create(<MyComponent />);',
' expect(testRenderer, \'to match snapshot\');'
].join('\n'));
});
});