From a4bd1bb76660b9970b95a92171135e7ba9119b6c Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 17 Oct 2021 17:39:50 +0200 Subject: [PATCH] Use built-in source map support for determining snapshot location Incidentally, this means the snap file is now properly tracked as a dependency. --- lib/runner.js | 33 +++++++++++++++++++++++---------- lib/snapshot-manager.js | 21 +++++++++------------ package-lock.json | 9 ++++++--- package.json | 1 - test-tap/api.js | 2 +- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 6dc403e83..24adebc56 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -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 = { @@ -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); } diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index b9750b831..e7a20acdf 100644 --- a/lib/snapshot-manager.js +++ b/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'; @@ -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}) => { diff --git a/package-lock.json b/package-lock.json index 573d827b5..59d54b352 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,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", @@ -3268,6 +3267,7 @@ "version": "1.8.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.8.0.tgz", "integrity": "sha512-+OQdjP49zViI/6i7nIJpA8rAl4sV/JdPfU9nZs3VqOwGIgizICvuN2ru6fMd+4llL0tar18UYJXfZ/TWtmhUjA==", + "dev": true, "dependencies": { "safe-buffer": "~5.1.1" } @@ -9183,7 +9183,8 @@ "node_modules/safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true }, "node_modules/safe-regex": { "version": "2.1.1", @@ -15607,6 +15608,7 @@ "version": "1.8.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.8.0.tgz", "integrity": "sha512-+OQdjP49zViI/6i7nIJpA8rAl4sV/JdPfU9nZs3VqOwGIgizICvuN2ru6fMd+4llL0tar18UYJXfZ/TWtmhUjA==", + "dev": true, "requires": { "safe-buffer": "~5.1.1" } @@ -19931,7 +19933,8 @@ "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true }, "safe-regex": { "version": "2.1.1", diff --git a/package.json b/package.json index 0035470bd..9a2cda43c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test-tap/api.js b/test-tap/api.js index 1502ba724..578fe5c26 100644 --- a/test-tap/api.js +++ b/test-tap/api.js @@ -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); } }); });