Skip to content

Commit a130a9e

Browse files
authored
Use source-mapped file to build snapshots
* Use the filename of the source file to build the snapshot filename * Put the relative path of the actual source file in the Markdown snapshot This is great for TypeScript projects where AVA is applied to pre-build files. The snapshots will be named `file.ts.snap`, and the relative path will be correct, rather than pointing at the output directory. This is a breaking change, since AVA will now write new snapshot files, without removing the previous ones. It may not catch bugs introduced at the same time as the project is updated to the new AVA version.
1 parent cc3e0ea commit a130a9e

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

lib/runner.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const path = require('path');
32
const matcher = require('matcher');
43
const ContextRef = require('./context-ref');
54
const createChain = require('./create-chain');
@@ -167,10 +166,7 @@ class Runner extends Emittery {
167166
this.snapshots = snapshotManager.load({
168167
file: this.file,
169168
fixedLocation: this.snapshotDir,
170-
name: path.basename(this.file),
171169
projectDir: this.projectDir,
172-
relFile: path.relative(this.projectDir, this.file),
173-
testDir: path.dirname(this.file),
174170
updating: this.updateSnapshots
175171
});
176172
this.emit('dependency', this.snapshots.snapPath);

lib/snapshot-manager.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,14 @@ class Manager {
359359
}
360360
}
361361

362-
function determineSnapshotDir(options) {
363-
const testDir = determineSourceMappedDir(options);
364-
if (options.fixedLocation) {
365-
const relativeTestLocation = path.relative(options.projectDir, testDir);
366-
return path.join(options.fixedLocation, relativeTestLocation);
362+
function determineSnapshotDir({file, fixedLocation, projectDir}) {
363+
const testDir = path.dirname(file);
364+
if (fixedLocation) {
365+
const relativeTestLocation = path.relative(projectDir, testDir);
366+
return path.join(fixedLocation, relativeTestLocation);
367367
}
368368

369-
const parts = new Set(path.relative(options.projectDir, testDir).split(path.sep));
369+
const parts = new Set(path.relative(projectDir, testDir).split(path.sep));
370370
if (parts.has('__tests__')) {
371371
return path.join(testDir, '__snapshots__');
372372
}
@@ -377,29 +377,37 @@ function determineSnapshotDir(options) {
377377
return testDir;
378378
}
379379

380-
function determineSourceMappedDir(options) {
381-
const source = tryRead(options.file).toString();
382-
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, options.testDir);
380+
function resolveSourceFile(file) {
381+
const testDir = path.dirname(file);
382+
const buffer = tryRead(file);
383+
if (!buffer) {
384+
return file; // Assume the file is stubbed in our test suite.
385+
}
386+
387+
const source = buffer.toString();
388+
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, testDir);
383389
if (converter) {
384390
const map = converter.toObject();
385391
const firstSource = `${map.sourceRoot || ''}${map.sources[0]}`;
386-
const sourceFile = path.resolve(options.testDir, firstSource);
387-
return path.dirname(sourceFile);
392+
return path.resolve(testDir, firstSource);
388393
}
389394

390-
return options.testDir;
395+
return file;
391396
}
392397

393-
function load(options) {
394-
const dir = determineSnapshotDir(options);
395-
const reportFile = `${options.name}.md`;
396-
const snapFile = `${options.name}.snap`;
398+
function load({file, fixedLocation, projectDir, updating}) {
399+
const sourceFile = resolveSourceFile(file);
400+
const dir = determineSnapshotDir({file: sourceFile, fixedLocation, projectDir});
401+
const relFile = path.relative(projectDir, sourceFile);
402+
const name = path.basename(relFile);
403+
const reportFile = `${name}.md`;
404+
const snapFile = `${name}.snap`;
397405
const snapPath = path.join(dir, snapFile);
398406

399-
let appendOnly = !options.updating;
407+
let appendOnly = !updating;
400408
let snapshotsByHash;
401409

402-
if (!options.updating) {
410+
if (!updating) {
403411
const buffer = tryRead(snapPath);
404412
if (buffer) {
405413
snapshotsByHash = decodeSnapshots(buffer, snapPath);
@@ -411,7 +419,7 @@ function load(options) {
411419
return new Manager({
412420
appendOnly,
413421
dir,
414-
relFile: options.relFile,
422+
relFile,
415423
reportFile,
416424
snapFile,
417425
snapPath,

test/assert.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,12 +1227,9 @@ test('.snapshot()', t => {
12271227

12281228
const projectDir = path.join(__dirname, 'fixture');
12291229
const manager = snapshotManager.load({
1230-
file: __filename,
1231-
name: 'assert.js',
1230+
file: path.join(projectDir, 'assert.js'),
12321231
projectDir,
1233-
relFile: 'test/assert.js',
12341232
fixedLocation: null,
1235-
testDir: projectDir,
12361233
updating
12371234
});
12381235
const setup = title => {

test/integration/snapshots.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ test('legacy snapshot files are reported to the console', t => {
129129
});
130130
});
131131

132-
test('snapshots infer their location from sourcemaps', t => {
132+
test('snapshots infer their location and name from sourcemaps', t => {
133133
t.plan(8);
134134
const relativeFixtureDir = path.join('fixture/snapshots/test-sourcemaps');
135135
const snapDirStructure = [
@@ -141,8 +141,8 @@ test('snapshots infer their location from sourcemaps', t => {
141141
.map(snapRelativeDir => {
142142
const snapPath = path.join(__dirname, '..', relativeFixtureDir, snapRelativeDir);
143143
return [
144-
path.join(snapPath, 'test.js.md'),
145-
path.join(snapPath, 'test.js.snap')
144+
path.join(snapPath, 'test.ts.md'),
145+
path.join(snapPath, 'test.ts.snap')
146146
];
147147
})
148148
.reduce((a, b) => a.concat(b), []);

test/test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,9 @@ test('assertions are bound', t => {
723723
test('snapshot assertion can be skipped', t => {
724724
const projectDir = path.join(__dirname, 'fixture');
725725
const manager = snapshotManager.load({
726-
file: __filename,
727-
name: 'assert.js',
726+
file: path.join(projectDir, 'assert.js'),
728727
projectDir,
729-
relFile: 'test/assert.js',
730728
fixedLocation: null,
731-
testDir: projectDir,
732729
updating: false
733730
});
734731

0 commit comments

Comments
 (0)