forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-esm-type-flag-package-scopes.mjs
167 lines (144 loc) · 7.4 KB
/
test-esm-type-flag-package-scopes.mjs
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
// Flags: --experimental-default-type=module --experimental-wasm-modules
import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { describe, it } from 'node:test';
import { strictEqual } from 'node:assert';
describe('the type flag should change the interpretation of certain files within a "type": "module" package scope',
{ concurrency: true }, () => {
it('should run as ESM an extensionless JavaScript file within a "type": "module" scope', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
fixtures.path('es-modules/package-type-module/noext-esm'),
]);
strictEqual(stderr, '');
strictEqual(stdout, 'executed\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
it('should import an extensionless JavaScript file within a "type": "module" scope', async () => {
const { default: defaultExport } =
await import(fixtures.fileURL('es-modules/package-type-module/noext-esm'));
strictEqual(defaultExport, 'module');
});
it('should import an extensionless JavaScript file within a "type": "module" scope under node_modules',
async () => {
const { default: defaultExport } =
await import(fixtures.fileURL(
'es-modules/package-type-module/node_modules/dep-with-package-json-type-module/noext-esm'));
strictEqual(defaultExport, 'module');
});
it('should run as Wasm an extensionless Wasm file within a "type": "module" scope', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
'--experimental-wasm-modules',
'--no-warnings',
fixtures.path('es-modules/package-type-module/noext-wasm'),
]);
strictEqual(stderr, '');
strictEqual(stdout, 'executed\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
it('should import as Wasm an extensionless Wasm file within a "type": "module" scope', async () => {
const { add } = await import(fixtures.fileURL('es-modules/package-type-module/noext-wasm'));
strictEqual(add(1, 2), 3);
});
it('should import an extensionless Wasm file within a "type": "module" scope under node_modules',
async () => {
const { add } = await import(fixtures.fileURL(
'es-modules/package-type-module/node_modules/dep-with-package-json-type-module/noext-wasm'));
strictEqual(add(1, 2), 3);
});
});
describe(`the type flag should change the interpretation of certain files within a package scope that lacks a
"type" field and is not under node_modules`, { concurrency: true }, () => {
it('should run as ESM a .js file within package scope that has no defined "type" and is not under node_modules',
async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
fixtures.path('es-modules/package-without-type/module.js'),
]);
strictEqual(stderr, '');
strictEqual(stdout, 'executed\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
it(`should run as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is not
under node_modules`, async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
fixtures.path('es-modules/package-without-type/noext-esm'),
]);
strictEqual(stderr, '');
strictEqual(stdout, 'executed\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
it(`should run as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not under
node_modules`, async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
'--experimental-wasm-modules',
'--no-warnings',
fixtures.path('es-modules/noext-wasm'),
]);
strictEqual(stderr, '');
strictEqual(stdout, '');
strictEqual(code, 0);
strictEqual(signal, null);
});
it('should import as ESM a .js file within package scope that has no defined "type" and is not under node_modules',
async () => {
const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/module.js'));
strictEqual(defaultExport, 'module');
});
it(`should import as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is
not under node_modules`, async () => {
const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/noext-esm'));
strictEqual(defaultExport, 'module');
});
it(`should import as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not
under node_modules`, async () => {
const { add } = await import(fixtures.fileURL('es-modules/noext-wasm'));
strictEqual(add(1, 2), 3);
});
});
describe(`the type flag should NOT change the interpretation of certain files within a package scope that lacks a
"type" field and is under node_modules`, { concurrency: true }, () => {
it('should run as CommonJS a .js file within package scope that has no defined "type" and is under node_modules',
async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json-without-type/run.js'),
]);
strictEqual(stderr, '');
strictEqual(stdout, 'executed\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
it(`should import as CommonJS a .js file within a package scope that has no defined "type" and is under
node_modules`, async () => {
const { default: defaultExport } =
await import(fixtures.fileURL(
'es-modules/package-type-module/node_modules/dep-with-package-json-without-type/run.js'));
strictEqual(defaultExport, 42);
});
it(`should run as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and is
under node_modules`, async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json-without-type/noext-cjs'),
]);
strictEqual(stderr, '');
strictEqual(stdout, 'executed\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
it(`should import as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and
is under node_modules`, async () => {
const { default: defaultExport } =
await import(fixtures.fileURL(
'es-modules/package-type-module/node_modules/dep-with-package-json-without-type/noext-cjs'));
strictEqual(defaultExport, 42);
});
});