forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-esm-import-attributes-identity.mjs
More file actions
59 lines (55 loc) · 2.17 KB
/
test-esm-import-attributes-identity.mjs
File metadata and controls
59 lines (55 loc) · 2.17 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
import '../common/index.mjs';
import assert from 'node:assert';
import Module from 'node:module';
// This test verifies that importing two modules with different import
// attributes should result in different module instances, if the module loader
// resolves to module instances.
//
// For example,
// ```mjs
// import * as secret1 from '../primitive-42.json' with { type: 'json' };
// import * as secret2 from '../primitive-42.json';
// ```
// should result in two different module instances, if the second import
// is been evaluated as a CommonJS module.
//
// ECMA-262 requires that in `HostLoadImportedModule`, if the operation is called
// multiple times with two (referrer, moduleRequest) pairs, it should return the same
// result. But if the moduleRequest is different, it should return a different,
// and the module loader resolves to different module instances, it should return
// different module instances.
// Refs: https://tc39.es/ecma262/#sec-HostLoadImportedModule
const kJsonModuleSpecifier = '../primitive-42.json';
const fixtureUrl = new URL('../fixtures/primitive-42.json', import.meta.url).href;
Module.registerHooks({
resolve: (specifier, context, nextResolve) => {
if (kJsonModuleSpecifier !== specifier) {
return nextResolve(specifier, context);
}
if (context.importAttributes.type === 'json') {
return {
url: fixtureUrl,
// Return a new importAttributes object to ensure
// that the module loader treats this as a same module request.
importAttributes: {
type: 'json',
},
shortCircuit: true,
format: 'json',
};
}
return {
url: fixtureUrl,
// Return a new importAttributes object to ensure
// that the module loader treats this as a same module request.
importAttributes: {},
shortCircuit: true,
format: 'module',
};
},
});
const { secretModule, secretJson, secretJson2 } = await import('../fixtures/es-modules/json-modules-attributes.mjs');
assert.notStrictEqual(secretModule, secretJson);
assert.strictEqual(secretJson, secretJson2);
assert.strictEqual(secretJson.default, 42);
assert.strictEqual(secretModule.default, undefined);