Skip to content

Commit d828d70

Browse files
committed
vfs: fix ESM legacyMainResolve tests to use bare specifier imports
ESM legacyMainResolve is only triggered via bare specifier package resolution, not directory path imports. Use VFS node_modules with wrapper .mjs modules that re-export from bare specifiers to properly exercise the VFS legacyMainResolve override.
1 parent a556bbf commit d828d70

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

test/parallel/test-vfs-require.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ const vfs = require('node:vfs');
291291
myVfs.unmount();
292292
}
293293

294-
// Test legacyMainResolve: package with "main" field resolves through VFS
294+
// Test CJS: package with "main" field resolves through VFS
295295
{
296296
const myVfs = vfs.create();
297297
myVfs.mkdirSync('/pkg/lib', { recursive: true });
@@ -308,7 +308,7 @@ const vfs = require('node:vfs');
308308
myVfs.unmount();
309309
}
310310

311-
// Test legacyMainResolve: package with no "main" field resolves index.js
311+
// Test CJS: package with no "main" field resolves index.js
312312
{
313313
const myVfs = vfs.create();
314314
myVfs.mkdirSync('/pkg2', { recursive: true });
@@ -324,6 +324,48 @@ const vfs = require('node:vfs');
324324
myVfs.unmount();
325325
}
326326

327+
// Test ESM legacyMainResolve: import() a VFS package with "main" (no "exports")
328+
// This triggers the ESM legacyMainResolve path in resolve.js via bare specifier
329+
{
330+
const myVfs = vfs.create();
331+
myVfs.mkdirSync('/app/node_modules/esm-legacy-main/lib', { recursive: true });
332+
myVfs.writeFileSync('/app/node_modules/esm-legacy-main/package.json', JSON.stringify({
333+
name: 'esm-legacy-main',
334+
type: 'module',
335+
main: './lib/entry.js',
336+
}));
337+
myVfs.writeFileSync('/app/node_modules/esm-legacy-main/lib/entry.js',
338+
'export const value = "esm-legacy-main";');
339+
myVfs.writeFileSync('/app/main.mjs',
340+
'export { value } from "esm-legacy-main";');
341+
myVfs.mount('/virtual20b');
342+
343+
import('/virtual20b/app/main.mjs').then(common.mustCall((mod) => {
344+
assert.strictEqual(mod.value, 'esm-legacy-main');
345+
myVfs.unmount();
346+
}));
347+
}
348+
349+
// Test ESM legacyMainResolve: import() a VFS package with no "main" (index.js fallback)
350+
{
351+
const myVfs = vfs.create();
352+
myVfs.mkdirSync('/app2/node_modules/esm-nomain', { recursive: true });
353+
myVfs.writeFileSync('/app2/node_modules/esm-nomain/package.json', JSON.stringify({
354+
name: 'esm-nomain',
355+
type: 'module',
356+
}));
357+
myVfs.writeFileSync('/app2/node_modules/esm-nomain/index.js',
358+
'export const value = "esm-index-fallback";');
359+
myVfs.writeFileSync('/app2/main.mjs',
360+
'export { value } from "esm-nomain";');
361+
myVfs.mount('/virtual21b');
362+
363+
import('/virtual21b/app2/main.mjs').then(common.mustCall((mod) => {
364+
assert.strictEqual(mod.value, 'esm-index-fallback');
365+
myVfs.unmount();
366+
}));
367+
}
368+
327369
// Test getFormatOfExtensionlessFile: extensionless JS file in type:module package
328370
{
329371
const myVfs = vfs.create();
@@ -335,9 +377,8 @@ const vfs = require('node:vfs');
335377
myVfs.writeFileSync('/esm-pkg/entry', 'export const x = 123;');
336378
myVfs.mount('/virtual22');
337379

338-
// Use import() to trigger ESM loader path
339-
const importPromise = import('/virtual22/esm-pkg/entry');
340-
importPromise.then(common.mustCall((mod) => {
380+
// Use import() to trigger ESM loader path for extensionless file detection
381+
import('/virtual22/esm-pkg/entry').then(common.mustCall((mod) => {
341382
assert.strictEqual(mod.x, 123);
342383
myVfs.unmount();
343384
}));

0 commit comments

Comments
 (0)