Skip to content

Commit d8b1e89

Browse files
authoredFeb 13, 2021
Remove support for snapshot IDs
1 parent 2426685 commit d8b1e89

File tree

12 files changed

+83
-91
lines changed

12 files changed

+83
-91
lines changed
 

‎docs/03-assertions.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,10 @@ Assert that `contents` matches `regex`.
322322
Assert that `contents` does not match `regex`.
323323

324324
### `.snapshot(expected, message?)`
325-
### `.snapshot(expected, options?, message?)`
326325

327-
Compares the `expected` value with a previously recorded snapshot. Snapshots are stored for each test, so ensure you give your tests unique titles. Alternatively pass an `options` object to select a specific snapshot, for instance `{id: 'my snapshot'}`.
326+
Compares the `expected` value with a previously recorded snapshot. Snapshots are stored for each test, so ensure you give your tests unique titles.
327+
328+
AVA 3 supports an `options` object that lets you select a specific snapshot, for instance `{id: 'my snapshot'}`. This is buggy and will be removed in AVA 4.
328329

329330
Snapshot assertions cannot be skipped when snapshots are being updated.
330331

‎index.d.ts

-16
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export type CommitDiscardOptions = {
3232
retainLogs?: boolean;
3333
};
3434

35-
/** Options that can be passed to the `t.snapshot()` assertion. */
36-
export type SnapshotOptions = {
37-
/** If provided and not an empty string, used to select the snapshot to compare the `expected` value against. */
38-
id?: string;
39-
};
40-
4135
export interface Assertions {
4236
/** Assert that `actual` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). Comes with power-assert. */
4337
assert: AssertAssertion;
@@ -241,18 +235,8 @@ export interface SnapshotAssertion {
241235
*/
242236
(expected: any, message?: string): void;
243237

244-
/**
245-
* Assert that `expected` is [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to a
246-
* previously recorded [snapshot](https://github.com/concordancejs/concordance#serialization-details) (selected
247-
* through `options.id` if provided), or if necessary record a new snapshot.
248-
*/
249-
(expected: any, options: SnapshotOptions, message?: string): void;
250-
251238
/** Skip this assertion. */
252239
skip(expected: any, message?: string): void;
253-
254-
/** Skip this assertion. */
255-
skip(expected: any, options: SnapshotOptions, message?: string): void;
256240
}
257241

258242
export interface ThrowsAssertion {

‎lib/assert.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ class Assertions {
698698
return handlePromise(retval, true);
699699
});
700700

701-
this.snapshot = withSkip((expected, ...rest) => {
701+
this.snapshot = withSkip((expected, message) => {
702702
if (disableSnapshots) {
703703
fail(new AssertionError({
704704
assertion: 'snapshot',
@@ -708,30 +708,33 @@ class Assertions {
708708
return;
709709
}
710710

711-
let message;
712-
let snapshotOptions;
713-
if (rest.length > 1) {
714-
[snapshotOptions, message] = rest;
715-
} else {
716-
const [optionsOrMessage] = rest;
717-
if (typeof optionsOrMessage === 'object') {
718-
snapshotOptions = optionsOrMessage;
719-
} else {
720-
message = optionsOrMessage;
721-
}
711+
if (message && message.id !== undefined) {
712+
fail(new AssertionError({
713+
assertion: 'snapshot',
714+
message: 'AVA 4 no longer supports snapshot IDs',
715+
improperUsage: true,
716+
values: [formatWithLabel('Called with id:', message.id)]
717+
}));
718+
return;
722719
}
723720

724721
if (!checkMessage('snapshot', message)) {
725722
return;
726723
}
727724

725+
if (message === '') {
726+
fail(new AssertionError({
727+
assertion: 'snapshot',
728+
improperUsage: true,
729+
message: 'The snapshot assertion message must be a non-empty string',
730+
values: [formatWithLabel('Called with:', message)]
731+
}));
732+
return;
733+
}
734+
728735
let result;
729736
try {
730-
result = compareWithSnapshot({
731-
expected,
732-
id: snapshotOptions ? snapshotOptions.id : undefined,
733-
message
734-
});
737+
result = compareWithSnapshot({expected, message});
735738
} catch (error) {
736739
if (!(error instanceof snapshotManager.SnapshotError)) {
737740
throw error;

‎lib/test.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,12 @@ class Test {
222222

223223
const deferRecording = this.metadata.inline;
224224
this.deferredSnapshotRecordings = [];
225-
this.compareWithSnapshot = ({expected, id, message}) => {
225+
this.compareWithSnapshot = ({expected, message}) => {
226226
this.snapshotCount++;
227227

228-
// TODO: In a breaking change, reject non-undefined, falsy IDs and messages.
229-
const belongsTo = id || snapshotBelongsTo;
230-
const index = id ? 0 : this.nextSnapshotIndex++;
231-
const label = id ? '' : message || `Snapshot ${index + 1}`; // Human-readable labels start counting at 1.
228+
const belongsTo = snapshotBelongsTo;
229+
const index = this.nextSnapshotIndex++;
230+
const label = message || `Snapshot ${index + 1}`; // Human-readable labels start counting at 1.
232231

233232
const {taskIndex, associatedTaskIndex} = this.metadata;
234233
const {record, ...result} = options.compareTestSnapshot({

‎test-d/snapshot.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {expectError} from 'tsd';
2+
import test from '..';
3+
4+
test('snapshot', t => {
5+
t.snapshot({foo: 'bar'});
6+
t.snapshot(null, 'a snapshot with a message');
7+
expectError(t.snapshot('hello world', null));
8+
});
9+
10+
test('snapshot.skip', t => {
11+
t.snapshot.skip({foo: 'bar'});
12+
t.snapshot.skip(null, 'a snapshot with a message');
13+
expectError(t.snapshot.skip('hello world', null));
14+
});

‎test-tap/assert.js

+32-17
Original file line numberDiff line numberDiff line change
@@ -1650,10 +1650,10 @@ test('.snapshot()', t => {
16501650
super({
16511651
compareWithSnapshot: assertionOptions => {
16521652
const {record, ...result} = manager.compare({
1653-
belongsTo: assertionOptions.id || this.title,
1653+
belongsTo: this.title,
16541654
expected: assertionOptions.expected,
1655-
index: assertionOptions.id ? 0 : this.snapshotInvocationCount++,
1656-
label: assertionOptions.id ? '' : assertionOptions.message || `Snapshot ${this.snapshotInvocationCount}`
1655+
index: this.snapshotInvocationCount++,
1656+
label: assertionOptions.message || `Snapshot ${this.snapshotInvocationCount}`
16571657
});
16581658
if (record) {
16591659
record();
@@ -1679,10 +1679,6 @@ test('.snapshot()', t => {
16791679
const {snapshot} = assertions;
16801680
snapshot({foo: 'bar'});
16811681
});
1682-
1683-
passes(t, () => {
1684-
assertions.snapshot({foo: 'bar'}, {id: 'fixed id'}, 'message not included in snapshot report');
1685-
});
16861682
}
16871683

16881684
{
@@ -1700,15 +1696,6 @@ test('.snapshot()', t => {
17001696
}
17011697
}
17021698

1703-
failsWith(t, () => {
1704-
const assertions = setup('fails (fixed id)');
1705-
assertions.snapshot({foo: 'not bar'}, {id: 'fixed id'}, 'different message, also not included in snapshot report');
1706-
}, {
1707-
assertion: 'snapshot',
1708-
message: 'different message, also not included in snapshot report',
1709-
values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]
1710-
});
1711-
17121699
{
17131700
const assertions = setup('fails');
17141701
if (updating) {
@@ -1727,7 +1714,7 @@ test('.snapshot()', t => {
17271714
{
17281715
const assertions = setup('bad message');
17291716
failsWith(t, () => {
1730-
assertions.snapshot(null, null, null);
1717+
assertions.snapshot(null, null);
17311718
}, {
17321719
assertion: 'snapshot',
17331720
improperUsage: true,
@@ -1737,6 +1724,34 @@ test('.snapshot()', t => {
17371724
formatted: /null/
17381725
}]
17391726
});
1727+
1728+
failsWith(t, () => {
1729+
assertions.snapshot(null, '');
1730+
}, {
1731+
assertion: 'snapshot',
1732+
improperUsage: true,
1733+
message: 'The snapshot assertion message must be a non-empty string',
1734+
values: [{
1735+
label: 'Called with:',
1736+
formatted: '\'\''
1737+
}]
1738+
});
1739+
}
1740+
1741+
{
1742+
// See https://github.com/avajs/ava/issues/2669
1743+
const assertions = setup('id');
1744+
failsWith(t, () => {
1745+
assertions.snapshot({foo: 'bar'}, {id: 'an id'});
1746+
}, {
1747+
assertion: 'snapshot',
1748+
improperUsage: true,
1749+
message: 'AVA 4 no longer supports snapshot IDs',
1750+
values: [{
1751+
label: 'Called with id:',
1752+
formatted: '\'an id\''
1753+
}]
1754+
});
17401755
}
17411756

17421757
manager.save();

‎test/snapshot-order/fixtures/randomness/test.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ test('B - declare some snapshots', async t => {
1010
t.snapshot(id(0));
1111
t.snapshot(id(1), 'has a message');
1212
t.snapshot(id(2), 'also has a message');
13-
t.snapshot(id(3), {id: 'has an ID'});
1413
});
1514

1615
test('A - declare some more snapshots', async t => {
@@ -36,12 +35,8 @@ test('E - discard some snapshots in a try()', async t => {
3635
t.snapshot(id(10), 'outer again');
3736
});
3837

39-
test('D - more snapshots with IDs', async t => {
38+
test('D - more snapshots', async t => {
4039
await randomDelay();
41-
t.snapshot(id(11), {id: 'the first in test D'});
4240
t.snapshot(id(12));
43-
// These have to be reported in reverse declaration order, because they can't
44-
// be reported under the same header
45-
t.snapshot(id(14), {id: 'the second-to-last in test D'});
4641
t.snapshot(id(13));
4742
});

‎test/snapshot-order/fixtures/randomness/test.js.md

+1-13
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ Generated by [AVA](https://avajs.dev).
1818
1919
'index: 2'
2020

21-
## has an ID
22-
23-
'index: 3'
24-
2521
## A - declare some more snapshots
2622

2723
> Snapshot 1
@@ -52,11 +48,7 @@ Generated by [AVA](https://avajs.dev).
5248
5349
'index: 10'
5450

55-
## the first in test D
56-
57-
'index: 11'
58-
59-
## D - more snapshots with IDs
51+
## D - more snapshots
6052

6153
> Snapshot 1
6254
@@ -65,7 +57,3 @@ Generated by [AVA](https://avajs.dev).
6557
> Snapshot 2
6658
6759
'index: 13'
68-
69-
## the second-to-last in test D
70-
71-
'index: 14'
Binary file not shown.

‎test/snapshot-order/fixtures/report-declaration-order/test.js

-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ test('B - declare some snapshots', t => {
66
t.snapshot(id(0));
77
t.snapshot(id(1), 'has a message');
88
t.snapshot(id(2), 'also has a message');
9-
t.snapshot(id(3), {id: 'has an ID'});
109
});
1110

1211
test('A - declare some more snapshots', t => {
@@ -30,10 +29,6 @@ test('E - discard some snapshots in a try()', async t => {
3029
});
3130

3231
test('D - more snapshots with IDs', t => {
33-
t.snapshot(id(11), {id: 'the first in test D'});
3432
t.snapshot(id(12));
35-
// These have to be reported in reverse declaration order, because they can't
36-
// be reported under the same header
37-
t.snapshot(id(14), {id: 'the second-to-last in test D'});
3833
t.snapshot(id(13));
3934
});

‎test/snapshot-order/snapshots/randomness.js.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ Generated by [AVA](https://avajs.dev).
99
> resulting snapshot in binary encoding
1010
1111
Buffer @Uint8Array [
12-
41564120 536e6170 73686f74 2076320a 02008017 6c36567a 01a19351 43f9bd06
13-
d5301f8b 08000000 00000003 63636460 e06090fc 96d9226e 3e3150e0 20d3b5ec
14-
8deb7702 0581401c 8863433f 89d6966f 7976ffe9 b56ded65 1a898c60 515d208e
15-
5f7b77a5 e07b763f b9ee6b93 72cfb0fe 63068b3a 43712414 e70371e9 bcb5a916
16-
dc227cea 5c3fa552 5852af32 82455b81 b8e980f6 f3c7a28b 4527ee67 8e3cf37e
17-
e62e46b0 e81c209e bcffafc6 d457fce1 e5e13782 366d5c15 c80416dd 0cc5a780
18-
f8922bf3 cddd07b7 a6b64e5c 51b5b3b7 4897092c fa008abf 03f11781 f9ea3e07
19-
e7db3ff3 792cbef3 817e0733 58949711 8295a1d8 12889919 d8c13e66 64651064
20-
e4cccc4b 49adb052 30344111 e6800a63 1735c52a 6a8655d4 1caba831 76471862
21-
1736c22e 6c8cd568 0bec8a0d b02ac62e 6a8855d4 08002fd4 dcd73f02 0000
12+
41564120 536e6170 73686f74 2076320a 020089ef 307bf8df 7d1d9bef fcd38d56
13+
76211f8b 08000000 00000003 dbc1c0c0 c0ca101b fa49b4b6 7ccbb3fb 4faf6d6b
14+
2fd34864 64000131 208e5f7b 77a5e07b 763fb9ee 6b9372cf b0fe6306 8bea40b1
15+
13144700 f1f94533 4f1d923e 71cf844d 86f7e85e 934626b0 683e14b7 01f12557
16+
e69bbb0f 6e4d6d9d b8a26a67 6f912e13 58740e14 6f06e22f 02f3d57d 0eceb77f
17+
e6f3587c e703fd0e 66b0e849 28be0fc5 5f819899 811dec46 46560641 468eccbc
18+
94d40a2b 0513aca2 a65845cd b08a9aa3 88724245 0d8db00b 1b6335c3 02bb6203
19+
ac8ab18b 1a621535 02004e86 b593ad01 0000
2220
]
-80 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)