From a130a9e3d9729e746b1ad9882ffc20de5b434750 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 28 Oct 2018 15:32:52 +0000 Subject: [PATCH] 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. --- lib/runner.js | 4 --- lib/snapshot-manager.js | 46 ++++++++++++++++++++--------------- test/assert.js | 5 +--- test/integration/snapshots.js | 6 ++--- test/test.js | 5 +--- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 62419c93a..67486b04f 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,5 +1,4 @@ 'use strict'; -const path = require('path'); const matcher = require('matcher'); const ContextRef = require('./context-ref'); const createChain = require('./create-chain'); @@ -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); diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index 69eed2120..cfb131acf 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -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__'); } @@ -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); @@ -411,7 +419,7 @@ function load(options) { return new Manager({ appendOnly, dir, - relFile: options.relFile, + relFile, reportFile, snapFile, snapPath, diff --git a/test/assert.js b/test/assert.js index cc47c8472..f3e4f1a2b 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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 => { diff --git a/test/integration/snapshots.js b/test/integration/snapshots.js index e76d5eec0..6564bc978 100644 --- a/test/integration/snapshots.js +++ b/test/integration/snapshots.js @@ -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 = [ @@ -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), []); diff --git a/test/test.js b/test/test.js index ca418328f..89f74f625 100644 --- a/test/test.js +++ b/test/test.js @@ -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 });