Skip to content

Commit 47f41a0

Browse files
committed
Improve new test
1 parent d646575 commit 47f41a0

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

test/es-module/test-esm-detect-ambiguous.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,16 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
411411
'import { pathToFileURL } from "node:url";' +
412412
'register("./transpile-esm-to-cjs.mjs", pathToFileURL("./"));'
413413
)}`,
414-
fixtures.path('es-modules/package-without-type/esm-with-check.js'),
414+
fixtures.path('es-modules/package-without-type/module.js'),
415415
], { cwd: fixtures.fileURL('es-module-loaders/') });
416416

417417
strictEqual(stderr, '');
418-
strictEqual(stdout, 'module\ncommonjs\ntransformed!\n');
418+
strictEqual(stdout, `
419+
Resolved format: module
420+
Loaded original format: module
421+
executed
422+
Evaluated format: commonjs
423+
`.replace(/^\s+/gm, ''));
419424
strictEqual(code, 0);
420425
strictEqual(signal, null);
421426
});

test/fixtures/es-module-loaders/transpile-esm-to-cjs.mjs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ import { writeSync } from "node:fs";
33
export async function resolve(specifier, context, next) {
44
const result = await next(specifier, context);
55
if (specifier.startsWith("file://")) {
6-
writeSync(1, result.format + '\n');
6+
writeSync(1, `Resolved format: ${result.format}\n`);
77
}
88
return result;
99
}
1010

1111
export async function load(url, context, next) {
1212
const output = await next(url, context);
13+
writeSync(1, `Loaded original format: ${output.format}\n`);
14+
1315
let source = `${output.source}`
1416

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

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

2123
output.source = source;
2224
output.format = 'commonjs';

test/fixtures/es-modules/package-without-type/esm-with-check.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)