Skip to content

Commit

Permalink
Use source-mapped file to build snapshots
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
novemberborn authored Oct 28, 2018
1 parent cc3e0ea commit a130a9e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
4 changes: 0 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const path = require('path');
const matcher = require('matcher');
const ContextRef = require('./context-ref');
const createChain = require('./create-chain');
Expand Down Expand Up @@ -167,10 +166,7 @@ class Runner extends Emittery {
this.snapshots = snapshotManager.load({
file: this.file,
fixedLocation: this.snapshotDir,
name: path.basename(this.file),
projectDir: this.projectDir,
relFile: path.relative(this.projectDir, this.file),
testDir: path.dirname(this.file),
updating: this.updateSnapshots
});
this.emit('dependency', this.snapshots.snapPath);
Expand Down
46 changes: 27 additions & 19 deletions lib/snapshot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,14 @@ class Manager {
}
}

function determineSnapshotDir(options) {
const testDir = determineSourceMappedDir(options);
if (options.fixedLocation) {
const relativeTestLocation = path.relative(options.projectDir, testDir);
return path.join(options.fixedLocation, relativeTestLocation);
function determineSnapshotDir({file, fixedLocation, projectDir}) {
const testDir = path.dirname(file);
if (fixedLocation) {
const relativeTestLocation = path.relative(projectDir, testDir);
return path.join(fixedLocation, relativeTestLocation);
}

const parts = new Set(path.relative(options.projectDir, testDir).split(path.sep));
const parts = new Set(path.relative(projectDir, testDir).split(path.sep));
if (parts.has('__tests__')) {
return path.join(testDir, '__snapshots__');
}
Expand All @@ -377,29 +377,37 @@ function determineSnapshotDir(options) {
return testDir;
}

function determineSourceMappedDir(options) {
const source = tryRead(options.file).toString();
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, options.testDir);
function resolveSourceFile(file) {
const testDir = path.dirname(file);
const buffer = tryRead(file);
if (!buffer) {
return file; // Assume the file is stubbed in our test suite.
}

const source = buffer.toString();
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, testDir);
if (converter) {
const map = converter.toObject();
const firstSource = `${map.sourceRoot || ''}${map.sources[0]}`;
const sourceFile = path.resolve(options.testDir, firstSource);
return path.dirname(sourceFile);
return path.resolve(testDir, firstSource);
}

return options.testDir;
return file;
}

function load(options) {
const dir = determineSnapshotDir(options);
const reportFile = `${options.name}.md`;
const snapFile = `${options.name}.snap`;
function load({file, fixedLocation, projectDir, updating}) {
const sourceFile = resolveSourceFile(file);
const dir = determineSnapshotDir({file: sourceFile, fixedLocation, projectDir});
const relFile = path.relative(projectDir, sourceFile);
const name = path.basename(relFile);
const reportFile = `${name}.md`;
const snapFile = `${name}.snap`;
const snapPath = path.join(dir, snapFile);

let appendOnly = !options.updating;
let appendOnly = !updating;
let snapshotsByHash;

if (!options.updating) {
if (!updating) {
const buffer = tryRead(snapPath);
if (buffer) {
snapshotsByHash = decodeSnapshots(buffer, snapPath);
Expand All @@ -411,7 +419,7 @@ function load(options) {
return new Manager({
appendOnly,
dir,
relFile: options.relFile,
relFile,
reportFile,
snapFile,
snapPath,
Expand Down
5 changes: 1 addition & 4 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,12 +1227,9 @@ test('.snapshot()', t => {

const projectDir = path.join(__dirname, 'fixture');
const manager = snapshotManager.load({
file: __filename,
name: 'assert.js',
file: path.join(projectDir, 'assert.js'),
projectDir,
relFile: 'test/assert.js',
fixedLocation: null,
testDir: projectDir,
updating
});
const setup = title => {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test('legacy snapshot files are reported to the console', t => {
});
});

test('snapshots infer their location from sourcemaps', t => {
test('snapshots infer their location and name from sourcemaps', t => {
t.plan(8);
const relativeFixtureDir = path.join('fixture/snapshots/test-sourcemaps');
const snapDirStructure = [
Expand All @@ -141,8 +141,8 @@ test('snapshots infer their location from sourcemaps', t => {
.map(snapRelativeDir => {
const snapPath = path.join(__dirname, '..', relativeFixtureDir, snapRelativeDir);
return [
path.join(snapPath, 'test.js.md'),
path.join(snapPath, 'test.js.snap')
path.join(snapPath, 'test.ts.md'),
path.join(snapPath, 'test.ts.snap')
];
})
.reduce((a, b) => a.concat(b), []);
Expand Down
5 changes: 1 addition & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,12 +723,9 @@ test('assertions are bound', t => {
test('snapshot assertion can be skipped', t => {
const projectDir = path.join(__dirname, 'fixture');
const manager = snapshotManager.load({
file: __filename,
name: 'assert.js',
file: path.join(projectDir, 'assert.js'),
projectDir,
relFile: 'test/assert.js',
fixedLocation: null,
testDir: projectDir,
updating: false
});

Expand Down

0 comments on commit a130a9e

Please sign in to comment.