Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to get tests to run on Windows #1

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 8 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
Expand All @@ -30,30 +30,30 @@ 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);

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);
Expand Down