Skip to content

Commit

Permalink
Improve new test
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Jun 25, 2024
1 parent f4d6382 commit bcdb35e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 7 additions & 2 deletions test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,16 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
'import { pathToFileURL } from "node:url";' +
'register("./transpile-esm-to-cjs.mjs", pathToFileURL("./"));'
)}`,
fixtures.path('es-modules/package-without-type/esm-with-check.js'),
fixtures.path('es-modules/package-without-type/module.js'),
], { cwd: fixtures.fileURL('es-module-loaders/') });

strictEqual(stderr, '');
strictEqual(stdout, 'module\ncommonjs\ntransformed!\n');
strictEqual(stdout, `
Resolved format: module
Loaded original format: module
executed
Evaluated format: commonjs
`.replace(/^\s+/gm, ''));
strictEqual(code, 0);
strictEqual(signal, null);
});
Expand Down
12 changes: 7 additions & 5 deletions test/fixtures/es-module-loaders/transpile-esm-to-cjs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import { writeSync } from "node:fs";
export async function resolve(specifier, context, next) {
const result = await next(specifier, context);
if (specifier.startsWith("file://")) {
writeSync(1, result.format + '\n');
writeSync(1, `Resolved format: ${result.format}\n`);
}
return result;
}

export async function load(url, context, next) {
const output = await next(url, context);
writeSync(1, `Loaded original format: ${output.format}\n`);

let source = `${output.source}`

// This is a very naive implementation for testing purposes only
if (source?.includes('import')) {
source = source.replace(/import\s+{\s*([^;]+)\s*}\s+from\s+(['"])(.*?)\2;/g, 'const { $1 } = require($2$3$2);')
// This is a very incomplete and naively done implementation for testing purposes only
if (source?.includes('export default')) {
source = source.replace('export default', 'module.exports =');

source += '\nconsole.log("transformed!");'; // To verify the hook ran
source += '\nconsole.log(`Evaluated format: ${this === undefined ? "module" : "commonjs"}`);';

output.source = source;
output.format = 'commonjs';
Expand Down

This file was deleted.

0 comments on commit bcdb35e

Please sign in to comment.