Repository navigation
Expand file tree
/
Copy pathtest-vfs-require.js
More file actions
411 lines (341 loc) · 11.8 KB
/
Copy pathtest-vfs-require.js
File metadata and controls
411 lines (341 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Flags: --experimental-vfs
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('node:url');
const vfs = require('node:vfs');
const vfsImport = (path) => pathToFileURL(path).href;
// Test requiring a simple virtual module
{
const myVfs = vfs.create();
myVfs.writeFileSync('/hello.js', 'module.exports = "hello from vfs";');
const mountPoint = myVfs.mount();
const result = require(`${mountPoint}/hello.js`);
assert.strictEqual(result, 'hello from vfs');
myVfs.unmount();
}
// Test requiring a virtual module that exports an object
{
const myVfs = vfs.create();
myVfs.writeFileSync('/config.js', `
module.exports = {
name: 'test-config',
version: '1.0.0',
getValue: function() { return 42; }
};
`);
const mountPoint = myVfs.mount();
const config = require(`${mountPoint}/config.js`);
assert.strictEqual(config.name, 'test-config');
assert.strictEqual(config.version, '1.0.0');
assert.strictEqual(config.getValue(), 42);
myVfs.unmount();
}
// Test requiring a virtual module that requires another virtual module.
// Mount first so the embedded absolute specifier can use the mount point.
{
const myVfs = vfs.create();
const mountPoint = myVfs.mount();
myVfs.writeFileSync(`${mountPoint}/utils.js`, `
module.exports = {
add: function(a, b) { return a + b; }
};
`);
myVfs.writeFileSync(`${mountPoint}/main.js`, `
const utils = require(${JSON.stringify(`${mountPoint}/utils.js`)});
module.exports = {
sum: utils.add(10, 20)
};
`);
const main = require(`${mountPoint}/main.js`);
assert.strictEqual(main.sum, 30);
myVfs.unmount();
}
// Test requiring a JSON file from VFS
{
const myVfs = vfs.create();
myVfs.writeFileSync('/data.json', JSON.stringify({
items: [1, 2, 3],
enabled: true,
}));
const mountPoint = myVfs.mount();
const data = require(`${mountPoint}/data.json`);
assert.deepStrictEqual(data.items, [1, 2, 3]);
assert.strictEqual(data.enabled, true);
myVfs.unmount();
}
// Test virtual package.json resolution
{
const myVfs = vfs.create();
myVfs.mkdirSync('/my-package', { recursive: true });
myVfs.writeFileSync('/my-package/package.json', JSON.stringify({
name: 'my-package',
main: 'index.js',
}));
myVfs.writeFileSync('/my-package/index.js', `
module.exports = { loaded: true };
`);
const mountPoint = myVfs.mount();
const pkg = require(`${mountPoint}/my-package`);
assert.strictEqual(pkg.loaded, true);
myVfs.unmount();
}
// Test that real modules still work when VFS is mounted
{
const myVfs = vfs.create();
myVfs.writeFileSync('/test.js', 'module.exports = 1;');
myVfs.mount();
assert.strictEqual(typeof assert.strictEqual, 'function');
const commonMod = require('../common');
assert.ok(commonMod);
myVfs.unmount();
}
// Test require with relative paths inside VFS module
{
const myVfs = vfs.create();
myVfs.mkdirSync('/lib', { recursive: true });
myVfs.writeFileSync('/lib/helper.js', `
module.exports = { help: function() { return 'helped'; } };
`);
myVfs.writeFileSync('/lib/index.js', `
const helper = require('./helper.js');
module.exports = helper.help();
`);
const mountPoint = myVfs.mount();
const result = require(`${mountPoint}/lib/index.js`);
assert.strictEqual(result, 'helped');
myVfs.unmount();
}
// Test fs.readFileSync interception when VFS is active
{
const myVfs = vfs.create();
myVfs.writeFileSync('/file.txt', 'virtual content');
const mountPoint = myVfs.mount();
const content = fs.readFileSync(`${mountPoint}/file.txt`, 'utf8');
assert.strictEqual(content, 'virtual content');
myVfs.unmount();
}
// Test requiring an ESM .mjs module from VFS
{
const myVfs = vfs.create();
myVfs.writeFileSync('/esm.mjs', 'export const msg = "hello from esm";');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/esm.mjs`);
assert.strictEqual(mod.msg, 'hello from esm');
myVfs.unmount();
}
// Test requiring an ESM .mjs module with default export from VFS
{
const myVfs = vfs.create();
myVfs.writeFileSync('/esm-default.mjs', 'export default function() { return 42; }');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/esm-default.mjs`);
assert.strictEqual(mod.default(), 42);
myVfs.unmount();
}
// Test require(esm): .js file detected as ESM via VFS package.json type:module
{
const myVfs = vfs.create();
myVfs.mkdirSync('/app', { recursive: true });
myVfs.writeFileSync('/app/package.json', JSON.stringify({
name: 'esm-app',
type: 'module',
}));
myVfs.writeFileSync('/app/lib.js',
'export const value = 42;' +
' export function hello() { return "hi"; }');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/app/lib.js`);
assert.strictEqual(mod.value, 42);
assert.strictEqual(mod.hello(), 'hi');
myVfs.unmount();
}
// Test require(esm): nested .js walks up to parent package.json in VFS
{
const myVfs = vfs.create();
myVfs.mkdirSync('/project/src/utils', { recursive: true });
myVfs.writeFileSync('/project/package.json', JSON.stringify({
name: 'nested-esm',
type: 'module',
}));
myVfs.writeFileSync('/project/src/utils/math.js',
'export const add = (a, b) => a + b;' +
' export default 99;');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/project/src/utils/math.js`);
assert.strictEqual(mod.add(3, 4), 7);
assert.strictEqual(mod.default, 99);
myVfs.unmount();
}
// Test require(esm): .js without type field stays CJS
{
const myVfs = vfs.create();
myVfs.mkdirSync('/cjs-app', { recursive: true });
myVfs.writeFileSync('/cjs-app/package.json', JSON.stringify({
name: 'cjs-app',
}));
myVfs.writeFileSync('/cjs-app/index.js',
'module.exports = { cjs: true };');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/cjs-app/index.js`);
assert.strictEqual(mod.cjs, true);
myVfs.unmount();
}
// Test require(esm): ESM .js that imports another ESM .js in VFS
{
const myVfs = vfs.create();
myVfs.mkdirSync('/multi/src', { recursive: true });
myVfs.writeFileSync('/multi/package.json', JSON.stringify({
type: 'module',
}));
myVfs.writeFileSync('/multi/src/dep.js', 'export const X = 100;');
myVfs.writeFileSync('/multi/src/main.js',
'import { X } from "./dep.js";' +
' export const result = X + 1;');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/multi/src/main.js`);
assert.strictEqual(mod.result, 101);
myVfs.unmount();
}
// Test require(esm): .mjs without any package.json loads as ESM
{
const myVfs = vfs.create();
myVfs.writeFileSync('/no-pkg.mjs',
'export const x = 1; export default "hello";');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/no-pkg.mjs`);
assert.strictEqual(mod.x, 1);
assert.strictEqual(mod.default, 'hello');
myVfs.unmount();
}
// Test require(esm): .mjs with package.json that has no type field
{
const myVfs = vfs.create();
myVfs.mkdirSync('/app', { recursive: true });
myVfs.writeFileSync('/app/package.json',
JSON.stringify({ name: 'no-type' }));
myVfs.writeFileSync('/app/lib.mjs', 'export const val = 42;');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/app/lib.mjs`);
assert.strictEqual(mod.val, 42);
myVfs.unmount();
}
// Test require(esm): .mjs in type:commonjs package still loads as ESM
{
const myVfs = vfs.create();
myVfs.mkdirSync('/cjs-pkg', { recursive: true });
myVfs.writeFileSync('/cjs-pkg/package.json', JSON.stringify({
name: 'cjs-pkg',
type: 'commonjs',
}));
myVfs.writeFileSync('/cjs-pkg/esm.mjs', 'export const z = 99;');
const mountPoint = myVfs.mount();
const mod = require(`${mountPoint}/cjs-pkg/esm.mjs`);
assert.strictEqual(mod.z, 99);
myVfs.unmount();
}
// Test CJS: package with "main" field resolves through VFS
{
const myVfs = vfs.create();
myVfs.mkdirSync('/pkg/lib', { recursive: true });
myVfs.writeFileSync('/pkg/package.json', JSON.stringify({
name: 'legacy-main-pkg',
main: './lib/entry',
}));
myVfs.writeFileSync('/pkg/lib/entry.js', 'module.exports = "legacy-main";');
const mountPoint = myVfs.mount();
const result = require(`${mountPoint}/pkg`);
assert.strictEqual(result, 'legacy-main');
myVfs.unmount();
}
// Test CJS: package with no "main" field resolves index.js
{
const myVfs = vfs.create();
myVfs.mkdirSync('/pkg2', { recursive: true });
myVfs.writeFileSync('/pkg2/package.json', JSON.stringify({
name: 'no-main-pkg',
}));
myVfs.writeFileSync('/pkg2/index.js', 'module.exports = "index-fallback";');
const mountPoint = myVfs.mount();
const result = require(`${mountPoint}/pkg2`);
assert.strictEqual(result, 'index-fallback');
myVfs.unmount();
}
// Test ESM legacyMainResolve: import() a VFS package with "main" (no "exports")
{
const myVfs = vfs.create();
myVfs.mkdirSync('/app/node_modules/esm-legacy-main/lib', { recursive: true });
myVfs.writeFileSync('/app/node_modules/esm-legacy-main/package.json', JSON.stringify({
name: 'esm-legacy-main',
type: 'module',
main: './lib/entry.js',
}));
myVfs.writeFileSync('/app/node_modules/esm-legacy-main/lib/entry.js',
'export const value = "esm-legacy-main";');
myVfs.writeFileSync('/app/main.mjs',
'export { value } from "esm-legacy-main";');
const mountPoint = myVfs.mount();
import(vfsImport(`${mountPoint}/app/main.mjs`)).then(common.mustCall((mod) => {
assert.strictEqual(mod.value, 'esm-legacy-main');
myVfs.unmount();
}));
}
// Test ESM legacyMainResolve: import() a VFS package with no "main" (index.js fallback)
{
const myVfs = vfs.create();
myVfs.mkdirSync('/app2/node_modules/esm-nomain', { recursive: true });
myVfs.writeFileSync('/app2/node_modules/esm-nomain/package.json', JSON.stringify({
name: 'esm-nomain',
type: 'module',
}));
myVfs.writeFileSync('/app2/node_modules/esm-nomain/index.js',
'export const value = "esm-index-fallback";');
myVfs.writeFileSync('/app2/main.mjs',
'export { value } from "esm-nomain";');
const mountPoint = myVfs.mount();
import(vfsImport(`${mountPoint}/app2/main.mjs`)).then(common.mustCall((mod) => {
assert.strictEqual(mod.value, 'esm-index-fallback');
myVfs.unmount();
}));
}
// Test getFormatOfExtensionlessFile: extensionless JS file in type:module package
{
const myVfs = vfs.create();
myVfs.mkdirSync('/esm-pkg', { recursive: true });
myVfs.writeFileSync('/esm-pkg/package.json', JSON.stringify({
name: 'esm-pkg',
type: 'module',
}));
myVfs.writeFileSync('/esm-pkg/entry', 'export const x = 123;');
const mountPoint = myVfs.mount();
import(vfsImport(`${mountPoint}/esm-pkg/entry`)).then(common.mustCall((mod) => {
assert.strictEqual(mod.x, 123);
myVfs.unmount();
}));
}
// Test that unmounting stops interception
{
const myVfs = vfs.create();
myVfs.writeFileSync('/unmount-test.js', 'module.exports = "before unmount";');
const mountPoint = myVfs.mount();
const result = require(`${mountPoint}/unmount-test.js`);
assert.strictEqual(result, 'before unmount');
myVfs.unmount();
// On Windows, the native loader may surface the NUL-backed former mount
// path as ERR_INVALID_PACKAGE_CONFIG while probing for package.json.
assert.throws(() => {
delete require.cache[path.join(mountPoint, 'unmount-test.js')];
require(`${mountPoint}/unmount-test.js`);
}, (err) => {
if (common.isWindows) {
assert.ok(
['ERR_INVALID_PACKAGE_CONFIG', 'MODULE_NOT_FOUND'].includes(err.code),
);
} else {
assert.strictEqual(err.code, 'MODULE_NOT_FOUND');
}
return true;
});
}