From 010c1be959a60e06b272f786dcaddedf04ddea49 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sat, 17 Feb 2024 08:22:44 -0700 Subject: [PATCH] Get the tests running on win32 --- index.js | 17 ++++++++++++----- package.json | 2 +- test/index.test.js | 16 ++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index a33b5f7..4da4617 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,7 @@ function requireString(code, dirname, options) { */ function resolveIfNeeded(dirname, specifier) { if (specifier.startsWith(".")) { - specifier = path.resolve(dirname, specifier); + specifier = new URL(specifier, dirname).toString(); } return specifier; } @@ -113,20 +113,27 @@ async function importString(code, dirname, options) { throw new Error("Requires node.js 20.8+ or 21."); } + const fileUrl = options.filename.startsWith("file:") + ? options.filename + : url.pathToFileURL(options.filename).toString(); + const dirUrl = dirname.startsWith("file:") + ? dirname + : url.pathToFileURL(dirname).toString() + "/"; + const mod = new vm.SourceTextModule(code, { - identifier: options.filename, + identifier: fileUrl, context: vm.createContext(options.context), initializeImportMeta(meta) { - meta.url = String(url.pathToFileURL(options.filename)); + meta.url = fileUrl; }, // @ts-expect-error Types in @types/node are wrong. importModuleDynamically(specifier) { - return import(resolveIfNeeded(dirname, specifier)); + return import(resolveIfNeeded(dirUrl, specifier)); }, }); await mod.link(async(specifier, referencingModule) => { - const resolvedSpecifier = resolveIfNeeded(dirname, specifier); + const resolvedSpecifier = resolveIfNeeded(dirUrl, specifier); const targetModule = await import(resolvedSpecifier); const exports = Object.keys(targetModule); diff --git a/package.json b/package.json index 4843f07..e3eea78 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "tsc", "lint": "eslint .", - "test": "c8 node --experimental-vm-modules --test" + "test": "c8 node --stack-trace-limit=1000 --experimental-vm-modules --test" }, "keywords": [ "vm", diff --git a/test/index.test.js b/test/index.test.js index 5901318..a691bac 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,9 +1,9 @@ "use strict"; const assert = require("node:assert"); -const test = require("node:test"); const fromMem = require("../index.js"); -const { pathToFileURL } = require("url"); +const { join } = require("node:path"); +const test = require("node:test"); test("options", async() => { assert.equal(typeof fromMem, "function"); @@ -20,7 +20,7 @@ test("commonjs", async() => { }); assert.equal(cjs, 4); await assert.rejects(() => fromMem("throw new Error('foo')", { - filename: `${__dirname}/test2.js`, + filename: join(__dirname, "test2.js"), format: "bare", }), (/** @type {Error} */ err) => { assert(/test2\.js/.test(err.stack), err.stack); @@ -30,7 +30,7 @@ test("commonjs", async() => { test("esm", async() => { const mjs4 = await fromMem("export default 5", { - filename: `${__dirname}/test4.mjs`, + filename: join(__dirname, "test4.js"), format: "es", }); assert.equal(mjs4.default, 5); @@ -38,22 +38,22 @@ test("esm", async() => { const mjs5 = await fromMem(` import {foo} from './fixtures/example.mjs'; export default foo();`, { - filename: `${__dirname}/test5.mjs`, + filename: join(__dirname, "test5.js"), format: "es", }); assert.equal(mjs5.default, 6); const mjs6 = await fromMem(` export default import.meta.url`, { - filename: `${__dirname}/test6.mjs`, + filename: join(__dirname, "test6.js"), format: "es", }); - assert.equal(mjs6.default, pathToFileURL(`${__dirname}/test6.mjs`)); + assert.match(mjs6.default, /test6\.js$/); const mjs7 = await fromMem(` const {foo} = await import('./fixtures/example.mjs'); export default foo();`, { - filename: `${__dirname}/test5.mjs`, + filename: join(__dirname, "test7.js"), format: "es", }); assert.equal(mjs7.default, 6);