Skip to content

Commit 0ee8fd6

Browse files
committed
test: add VFS bare specifier ESM resolution test
Add a test that exercises ESM bare specifier resolution (e.g., import 'my-vfs-pkg') from a node_modules directory inside VFS. This ensures the internalModuleStat wrapper and package.json reader hooks correctly handle package resolution for virtual file system mounts.
1 parent 4a18510 commit 0ee8fd6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/parallel/test-vfs-import.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,30 @@ import vfs from 'node:vfs';
108108

109109
myVfs.unmount();
110110
}
111+
112+
// Test ESM bare specifier resolution from VFS node_modules.
113+
// This sets up a proper node_modules structure inside VFS and imports
114+
// using a bare specifier (e.g., import 'my-vfs-pkg') instead of an
115+
// absolute path. This exercises the ESM default resolver's
116+
// internalModuleStat and getPackageJSONURL code paths.
117+
{
118+
const myVfs = vfs.create();
119+
myVfs.mkdirSync('/app/node_modules/my-vfs-pkg', { recursive: true });
120+
myVfs.writeFileSync('/app/node_modules/my-vfs-pkg/package.json', JSON.stringify({
121+
name: 'my-vfs-pkg',
122+
type: 'module',
123+
exports: { '.': './index.mjs' },
124+
}));
125+
myVfs.writeFileSync('/app/node_modules/my-vfs-pkg/index.mjs',
126+
'export const fromVfs = true;');
127+
// The importing module must also live inside the VFS mount so that
128+
// node_modules resolution walks upward from a VFS path.
129+
myVfs.writeFileSync('/app/entry.mjs',
130+
"export { fromVfs } from 'my-vfs-pkg';");
131+
myVfs.mount('/virtual9');
132+
133+
const { fromVfs } = await import('/virtual9/app/entry.mjs');
134+
assert.strictEqual(fromVfs, true);
135+
136+
myVfs.unmount();
137+
}

0 commit comments

Comments
 (0)