Skip to content

Commit

Permalink
Use built-in source map support for determining snapshot location
Browse files Browse the repository at this point in the history
Incidentally, this means the snap file is now properly tracked as a
dependency.
  • Loading branch information
novemberborn committed Oct 17, 2021
1 parent 70c51f0 commit a4bd1bb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
33 changes: 23 additions & 10 deletions lib/runner.js
Expand Up @@ -34,16 +34,6 @@ export default class Runner extends Emittery {
this.boundCompareTestSnapshot = this.compareTestSnapshot.bind(this);
this.boundSkipSnapshot = this.skipSnapshot.bind(this);
this.interrupted = false;
this.snapshots = loadSnapshots({
file: this.file,
fixedLocation: this.snapshotDir,
projectDir: this.projectDir,
recordNewSnapshots: this.recordNewSnapshots,
updating: this.updateSnapshots,
});
if (this.snapshots.snapPath !== undefined) {
this.emit('dependency', this.snapshots.snapPath);
}

this.nextTaskIndex = 0;
this.tasks = {
Expand Down Expand Up @@ -203,6 +193,29 @@ export default class Runner extends Emittery {
}, meta);
}

get snapshots() {
if (this._snapshots) {
return this._snapshots;
}

// Lazy load not when the runner is instantiated but when snapshots are
// needed. This should be after the test file has been loaded and source
// maps are available.
const snapshots = loadSnapshots({
file: this.file,
fixedLocation: this.snapshotDir,
projectDir: this.projectDir,
recordNewSnapshots: this.recordNewSnapshots,
updating: this.updateSnapshots,
});
if (snapshots.snapPath !== undefined) {
this.emit('dependency', snapshots.snapPath);
}

this._snapshots = snapshots;
return snapshots;
}

compareTestSnapshot(options) {
return this.snapshots.compare(options);
}
Expand Down
21 changes: 9 additions & 12 deletions lib/snapshot-manager.js
@@ -1,12 +1,13 @@
import {Buffer} from 'node:buffer';
import crypto from 'node:crypto';
import fs from 'node:fs';
import {findSourceMap} from 'node:module';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import zlib from 'node:zlib';

import cbor from 'cbor';
import concordance from 'concordance';
import convertSourceMap from 'convert-source-map';
import indentString from 'indent-string';
import mem from 'mem';
import slash from 'slash';
Expand Down Expand Up @@ -384,21 +385,17 @@ class Manager {
}

const resolveSourceFile = mem(file => {
const testDir = path.dirname(file);
const buffer = tryRead(file);
if (!buffer) {
return file; // Assume the file is stubbed in our test suite.
const sourceMap = findSourceMap(file);
if (sourceMap === undefined) {
return file;
}

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]}`;
return path.resolve(testDir, firstSource);
const {payload} = sourceMap;
if (payload.sources.length === 0) { // Hypothetical?
return file;
}

return file;
return fileURLToPath(payload.sources[0]);
});

export const determineSnapshotDir = mem(({file, fixedLocation, projectDir}) => {
Expand Down
9 changes: 6 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -88,7 +88,6 @@
"code-excerpt": "^3.0.0",
"common-path-prefix": "^3.0.0",
"concordance": "^5.0.4",
"convert-source-map": "^1.8.0",
"currently-unhandled": "^0.4.1",
"debug": "^4.3.2",
"del": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test-tap/api.js
Expand Up @@ -463,7 +463,7 @@ for (const opt of opts) {
plan.status.on('stateChange', evt => {
if (evt.type === 'dependencies') {
t.ok(testFiles.has(evt.testFile));
t.strictSame(evt.dependencies.slice(-3), sourceFiles);
t.strictSame(evt.dependencies.filter(dep => !dep.endsWith('.snap')).slice(-3), sourceFiles);
}
});
});
Expand Down

0 comments on commit a4bd1bb

Please sign in to comment.