Skip to content

Commit 7dcb928

Browse files
committed
Test behavior with existing unsorted snapshots
1 parent c7f63c4 commit 7dcb928

File tree

9 files changed

+202
-8
lines changed

9 files changed

+202
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const test = require('ava');
2+
3+
const id = index => `index: ${index}`;
4+
5+
let resolveDelay;
6+
const delay = new Promise(resolve => {
7+
resolveDelay = resolve;
8+
});
9+
10+
test('B - declared first, resolves second', async t => {
11+
await delay;
12+
t.snapshot(id(1));
13+
});
14+
15+
test('A - declared second, resolves first', t => {
16+
t.snapshot(id(2));
17+
resolveDelay();
18+
});
19+
20+
test('D - a new test, declared third', t => {
21+
t.snapshot(id(3));
22+
});
23+
24+
test('C - another new test, declared fourth', t => {
25+
t.snapshot(id(4));
26+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const test = require('ava');
2+
3+
const id = index => `index: ${index}`;
4+
5+
let resolveDelay;
6+
const delay = new Promise(resolve => {
7+
resolveDelay = resolve;
8+
});
9+
10+
test('B - declared first, resolves second', async t => {
11+
await delay;
12+
t.snapshot(id(1));
13+
});
14+
15+
test('A - declared second, resolves first', t => {
16+
t.snapshot(id(2));
17+
resolveDelay();
18+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Snapshot report for `test.js`
2+
3+
The actual snapshot is saved in `test.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## A - declared second, resolves first
8+
9+
> Snapshot 1
10+
11+
'index: 2'
12+
13+
## B - declared first, resolves second
14+
15+
> Snapshot 1
16+
17+
'index: 1'
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const test = require('ava');
2+
3+
const id = index => `index: ${index}`;
4+
5+
let resolveDelay;
6+
const delay = new Promise(resolve => {
7+
resolveDelay = resolve;
8+
});
9+
10+
test('B - declared first, resolves second', async t => {
11+
await delay;
12+
t.snapshot(id(1));
13+
});
14+
15+
test('A - declared second, resolves first', t => {
16+
t.snapshot(id(2));
17+
resolveDelay();
18+
});

test/snapshot-order/test.js

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ test('snapshot files are independent of test resolution order', async t => {
3333
t.deepEqual(snapshot, snapshotReversed);
3434
});
3535

36+
function getSnapshotIds(report) {
37+
function * matchAll(string, regexp) {
38+
let match;
39+
while ((match = regexp.exec(string)) !== null) {
40+
yield match;
41+
}
42+
}
43+
44+
const ids = [...matchAll(report, /'index: ([-.\d]+)'/g)].map(match => Number(match[1]));
45+
46+
return ids;
47+
}
48+
3649
test('snapshot reports are sorted in declaration order', async t => {
3750
const options = {
3851
cwd: exec.cwd('report-declaration-order'),
@@ -44,16 +57,115 @@ test('snapshot reports are sorted in declaration order', async t => {
4457
await exec.fixture(['--update-snapshots'], options);
4558

4659
const report = fs.readFileSync(path.join(options.cwd, 'test.js.md'), {encoding: 'utf8'});
60+
const ids = getSnapshotIds(report);
4761

48-
function * matchAll(string, regexp) {
49-
let match;
50-
while ((match = regexp.exec(string)) !== null) {
51-
yield match;
62+
t.deepEqual(ids, [...ids].sort((a, b) => a - b));
63+
});
64+
65+
const unsortedSnapshotPath = path.join(__dirname, 'fixtures', 'backwards-compatibility', 'unsorted', 'test.js.snap');
66+
const unsortedReportPath = path.join(__dirname, 'fixtures', 'backwards-compatibility', 'unsorted', 'test.js.md');
67+
68+
const unsortedSnapshot = fs.readFileSync(unsortedSnapshotPath);
69+
const unsortedReport = fs.readFileSync(unsortedReportPath);
70+
71+
test('unsorted snapshots are unchanged when checking', async t => {
72+
const options = {
73+
cwd: exec.cwd('backwards-compatibility', 'check-only'),
74+
env: {
75+
AVA_FORCE_CI: 'not-ci'
5276
}
53-
}
77+
};
5478

55-
const ids = [...matchAll(report, /'index: ([-.\d]+)'/g)].map(match => Number(match[1]));
56-
const sortedIds = [...ids].sort((a, b) => a - b);
79+
const snapshotPath = path.join(options.cwd, 'test.js.snap');
80+
const reportPath = path.join(options.cwd, 'test.js.md');
81+
82+
// Install a known-unsorted snapshot, report
83+
fs.copyFileSync(unsortedSnapshotPath, snapshotPath);
84+
fs.copyFileSync(unsortedReportPath, reportPath);
85+
86+
// Run test
87+
await exec.fixture([], options);
88+
89+
// Assert snapshot, report are unchanged
90+
const snapshot = fs.readFileSync(snapshotPath);
91+
const report = fs.readFileSync(reportPath);
92+
93+
t.deepEqual(snapshot, unsortedSnapshot);
94+
t.deepEqual(report, unsortedReport);
95+
96+
// Clean up the snapshot, report
97+
fs.unlinkSync(snapshotPath);
98+
fs.unlinkSync(reportPath);
99+
});
100+
101+
test('unsorted snapshots are changed when appending', async t => {
102+
const options = {
103+
cwd: exec.cwd('backwards-compatibility', 'append-only'),
104+
env: {
105+
AVA_FORCE_CI: 'not-ci'
106+
}
107+
};
108+
109+
const snapshotPath = path.join(options.cwd, 'test.js.snap');
110+
const reportPath = path.join(options.cwd, 'test.js.md');
111+
112+
// Install a known-unsorted snapshot, report
113+
fs.copyFileSync(unsortedSnapshotPath, snapshotPath);
114+
fs.copyFileSync(unsortedReportPath, reportPath);
115+
116+
// Run test
117+
await exec.fixture([], options);
118+
119+
// Assert snapshot, report are changed
120+
const snapshot = fs.readFileSync(snapshotPath);
121+
const report = fs.readFileSync(reportPath);
122+
123+
t.notDeepEqual(snapshot, unsortedSnapshot);
124+
t.notDeepEqual(report, unsortedReport);
125+
126+
// Assert entries appended to report are sorted
127+
const textReport = report.toString();
128+
const ids = getSnapshotIds(textReport);
129+
130+
t.deepEqual(ids, [2, 1, 3, 4]);
131+
132+
// Clean up the snapshot, report
133+
fs.unlinkSync(snapshotPath);
134+
fs.unlinkSync(reportPath);
135+
});
136+
137+
test('unsorted snapshots are changed when updating', async t => {
138+
const options = {
139+
cwd: exec.cwd('backwards-compatibility', 'updating'),
140+
env: {
141+
AVA_FORCE_CI: 'not-ci'
142+
}
143+
};
144+
145+
const snapshotPath = path.join(options.cwd, 'test.js.snap');
146+
const reportPath = path.join(options.cwd, 'test.js.md');
147+
148+
// Install a known-unsorted snapshot, report
149+
fs.copyFileSync(unsortedSnapshotPath, snapshotPath);
150+
fs.copyFileSync(unsortedReportPath, reportPath);
151+
152+
// Run test
153+
await exec.fixture(['--update-snapshots'], options);
154+
155+
// Assert snapshot, report are changed
156+
const snapshot = fs.readFileSync(snapshotPath);
157+
const report = fs.readFileSync(reportPath);
158+
159+
t.notDeepEqual(snapshot, unsortedSnapshot);
160+
t.notDeepEqual(report, unsortedReport);
161+
162+
// Assert report is sorted
163+
const textReport = report.toString();
164+
const ids = getSnapshotIds(textReport);
165+
166+
t.deepEqual(ids, [1, 2]);
57167

58-
t.deepEqual(ids, sortedIds);
168+
// Clean up the snapshot, report
169+
fs.unlinkSync(snapshotPath);
170+
fs.unlinkSync(reportPath);
59171
});

0 commit comments

Comments
 (0)