diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 21e55ce..21ab477 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [16.0.0, 18, 20] + node-version: [16.2.0, 18, 20] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} diff --git a/bin/doctest b/bin/doctest index cfaab88..4e1a7ba 100755 --- a/bin/doctest +++ b/bin/doctest @@ -11,7 +11,8 @@ const flags = idx >= 0 && idx < args.length - 1; require ('child_process') .spawn ( process.execPath, - ['--experimental-vm-modules', + ['--experimental-import-meta-resolve', + '--experimental-vm-modules', ...(flags ? args[idx + 1].split (/\s+/) : []), '--', path.resolve (__dirname, '..', 'lib', 'command.js'), diff --git a/lib/doctest.js b/lib/doctest.js index 79125d7..a037ed7 100644 --- a/lib/doctest.js +++ b/lib/doctest.js @@ -10,6 +10,7 @@ import fs from 'node:fs/promises'; import {dirname, resolve} from 'node:path'; +import url from 'node:url'; import vm from 'node:vm'; import * as acorn from 'acorn'; @@ -418,14 +419,16 @@ const run = queue => return [{lines: {input: input.lines, output: output.lines}, comparison}]; }); -const evaluateModule = async source => { +const evaluateModule = moduleUrl => async source => { const queue = []; const enqueue = io => { queue.push (io); }; const __doctest = {enqueue}; const context = vm.createContext ({...global, __doctest}); const module = new vm.SourceTextModule (source, {context}); await module.link (async (specifier, referencingModule) => { - const entries = Object.entries (await import (specifier)); + // import.meta.resolve returned a promise prior to Node.js v20.0.0. + const importUrl = await import.meta.resolve (specifier, moduleUrl); + const entries = Object.entries (await import (importUrl)); const module = new vm.SyntheticModule ( entries.map (([name]) => name), () => { @@ -496,18 +499,18 @@ const test = options => path => rewrite => async evaluate => { }; export default options => async path => { + const __filename = resolve (process.cwd (), path); let context = {}; switch (options.module) { case 'esm': { return test (options) (path) (rewriteJs ('module')) - (evaluateModule); + (evaluateModule (url.pathToFileURL (__filename))); } case 'commonjs': { const exports = {}; const module = {exports}; - const __filename = resolve (process.cwd (), path); const __dirname = dirname (__filename); context = {process, exports, module, require, __dirname, __filename}; } // fall through diff --git a/package.json b/package.json index 52ec0ff..3126abb 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "url": "git://github.com/davidchambers/doctest.git" }, "engines": { - "node": ">=16.0.0" + "node": ">=16.2.0" }, "dependencies": { "acorn": "8.11.x",