/
snapshots.js
281 lines (246 loc) · 8.79 KB
/
snapshots.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
'use strict';
const fs = require('fs');
const path = require('path');
const execa = require('execa');
const uniqueTempDir = require('unique-temp-dir');
const {test} = require('tap');
const {execCli} = require('../helper/cli');
const overrideCIChecks = {
CI: '',
TRAVIS: '',
CONTINUOUS_INTEGRATION: ''
};
for (const obj of [
{type: 'colocated', rel: '', dir: ''},
{type: '__tests__', rel: '__tests__-dir', dir: '__tests__/__snapshots__'},
{type: 'test', rel: 'test-dir', dir: 'test/snapshots'},
{type: 'tests', rel: 'tests-dir', dir: 'tests/snapshots'}
]) {
test(`snapshots work (${obj.type})`, t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', obj.rel, obj.dir, 'test.js.snap');
try {
fs.unlinkSync(snapPath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
const dirname = path.join('fixture/snapshots', obj.rel);
// Test should pass, and a snapshot gets written
execCli(['--update-snapshots', '--verbose'], {dirname, env: overrideCIChecks}, error => {
t.ifError(error);
t.true(fs.existsSync(snapPath));
// Test should pass, and the snapshot gets used
execCli([], {dirname}, error => {
t.ifError(error);
t.end();
});
});
});
}
test('appends to existing snapshots', t => {
const cliPath = require.resolve('../../cli.js');
const avaPath = require.resolve('../../');
const cwd = uniqueTempDir({create: true});
fs.writeFileSync(path.join(cwd, 'package.json'), '{}');
const initial = `import test from ${JSON.stringify(avaPath)}
test('one', t => {
t.snapshot({one: true})
})`;
fs.writeFileSync(path.join(cwd, 'test.js'), initial);
const run = () => execa(process.execPath, [cliPath, '--verbose', '--no-color'], {cwd, env: overrideCIChecks, reject: false});
return run().then(result => {
t.match(result.stdout, /1 test passed/);
fs.writeFileSync(path.join(cwd, 'test.js'), `${initial}
test('two', t => {
t.snapshot({two: true})
})`);
return run();
}).then(result => {
t.match(result.stdout, /2 tests passed/);
fs.writeFileSync(path.join(cwd, 'test.js'), `${initial}
test('two', t => {
t.snapshot({two: false})
})`);
return run();
}).then(result => {
t.match(result.stdout, /1 test failed/);
});
});
test('outdated snapshot version is reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x00, 0x00]));
execCli(['test.js'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
t.ok(error);
t.match(stdout, /The snapshot file is v0, but only v2 is supported\./);
t.match(stdout, /File path:/);
t.match(stdout, snapPath);
t.match(stdout, /Please run AVA again with the .*--update-snapshots.* flag to upgrade\./);
t.end();
});
});
test('newer snapshot version is reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0xFF, 0xFF]));
execCli(['test.js'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
t.ok(error);
t.match(stdout, /The snapshot file is v65535, but only v2 is supported\./);
t.match(stdout, /File path:/);
t.match(stdout, snapPath);
t.match(stdout, /You should upgrade AVA\./);
t.end();
});
});
test('snapshot corruption is reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x02, 0x00]));
execCli(['test.js'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
t.ok(error);
t.match(stdout, /The snapshot file is corrupted\./);
t.match(stdout, /File path:/);
t.match(stdout, snapPath);
t.match(stdout, /Please run AVA again with the .*--update-snapshots.* flag to recreate it\./);
t.end();
});
});
test('legacy snapshot files are reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from('// Jest Snapshot v1, https://goo.gl/fbAQLP\n'));
execCli(['test.js'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
t.ok(error);
t.match(stdout, /The snapshot file was created with AVA 0\.19\. It's not supported by this AVA version\./);
t.match(stdout, /File path:/);
t.match(stdout, snapPath);
t.match(stdout, /Please run AVA again with the .*--update-snapshots.* flag to upgrade\./);
t.end();
});
});
test('snapshots infer their location and name from sourcemaps', t => {
t.plan(8);
const relativeFixtureDir = path.join('fixture/snapshots/test-sourcemaps');
const snapDirStructure = [
'src',
'src/test/snapshots',
'src/feature/__tests__/__snapshots__'
];
const snapFixtureFilePaths = snapDirStructure
.map(snapRelativeDir => {
const snapPath = path.join(__dirname, '..', relativeFixtureDir, snapRelativeDir);
return [
path.join(snapPath, 'test.ts.md'),
path.join(snapPath, 'test.ts.snap')
];
})
.reduce((a, b) => a.concat(b), []);
const removeExistingSnapFixtureFiles = snapPath => {
try {
fs.unlinkSync(snapPath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
};
snapFixtureFilePaths.forEach(x => removeExistingSnapFixtureFiles(x));
const verifySnapFixtureFiles = relFilePath => {
t.true(fs.existsSync(relFilePath));
};
execCli(['--verbose'], {dirname: relativeFixtureDir, env: overrideCIChecks}, (error, stdout) => {
t.ifError(error);
snapFixtureFilePaths.forEach(x => verifySnapFixtureFiles(x));
t.match(stdout, /6 tests passed/);
t.end();
});
});
test('snapshots resolved location from "snapshotDir" in AVA config', t => {
t.plan(8);
const relativeFixtureDir = 'fixture/snapshots/test-snapshot-location';
const snapDir = 'snapshot-fixtures';
const snapDirStructure = [
'src',
'src/feature',
'src/feature/nested-feature'
];
const snapFixtureFilePaths = snapDirStructure
.map(snapRelativeDir => {
const snapPath = path.join(__dirname, '..', relativeFixtureDir, snapDir, snapRelativeDir);
return [
path.join(snapPath, 'test.js.md'),
path.join(snapPath, 'test.js.snap')
];
})
.reduce((a, b) => a.concat(b), []);
const removeExistingSnapFixtureFiles = snapPath => {
try {
fs.unlinkSync(snapPath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
};
snapFixtureFilePaths.forEach(x => removeExistingSnapFixtureFiles(x));
const verifySnapFixtureFiles = relFilePath => {
t.true(fs.existsSync(relFilePath));
};
execCli(['--verbose'], {dirname: relativeFixtureDir, env: overrideCIChecks}, (error, stdout) => {
t.ifError(error);
snapFixtureFilePaths.forEach(x => verifySnapFixtureFiles(x));
t.match(stdout, /6 tests passed/);
t.end();
});
});
test('snapshots are indentical on different platforms', t => {
const fixtureDir = path.join(__dirname, '..', 'fixture', 'snapshots', 'test-content');
const reportPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.md');
const snapPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.snap');
const expectedReportPath = path.join(fixtureDir, 'test.js.md.expected');
const expectedSnapPath = path.join(fixtureDir, 'test.js.snap.expected');
const removeFile = filePath => {
try {
fs.unlinkSync(filePath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
};
// Clear current snapshots
[reportPath, snapPath].forEach(fp => removeFile(fp));
// Test should pass, and a snapshot gets written
execCli(['--update-snapshots', '--verbose'], {dirname: fixtureDir, env: overrideCIChecks}, error => {
t.ifError(error);
t.true(fs.existsSync(reportPath));
t.true(fs.existsSync(snapPath));
const reportContents = fs.readFileSync(reportPath);
const snapContents = fs.readFileSync(snapPath);
const expectedReportContents = fs.readFileSync(expectedReportPath);
const expectedSnapContents = fs.readFileSync(expectedSnapPath);
t.true(reportContents.equals(expectedReportContents), 'report file contents matches snapshot');
t.true(snapContents.equals(expectedSnapContents), 'snap file contents matches snapshot');
t.end();
});
});
test('in CI, new snapshots are not recorded', t => {
const fixtureDir = path.join(__dirname, '..', 'fixture', 'snapshots', 'test-content');
const reportPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.md');
const snapPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.snap');
const removeFile = filePath => {
try {
fs.unlinkSync(filePath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
};
// Clear current snapshots
[reportPath, snapPath].forEach(fp => removeFile(fp));
// Test should fail, no snapshot gets written
execCli([], {dirname: fixtureDir}, (_, stdout) => {
t.match(stdout, 'No snapshot available — new snapshots are not created in CI environments');
t.false(fs.existsSync(reportPath));
t.false(fs.existsSync(snapPath));
t.end();
});
});