• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --experimental-default-type=module --experimental-wasm-modules
2import { spawnPromisified } from '../common/index.mjs';
3import * as fixtures from '../common/fixtures.mjs';
4import { describe, it } from 'node:test';
5import { strictEqual } from 'node:assert';
6
7describe('the type flag should change the interpretation of certain files within a "type": "module" package scope',
8         { concurrency: true }, () => {
9           it('should run as ESM an extensionless JavaScript file within a "type": "module" scope', async () => {
10             const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
11               '--experimental-default-type=module',
12               fixtures.path('es-modules/package-type-module/noext-esm'),
13             ]);
14
15             strictEqual(stderr, '');
16             strictEqual(stdout, 'executed\n');
17             strictEqual(code, 0);
18             strictEqual(signal, null);
19           });
20
21           it('should import an extensionless JavaScript file within a "type": "module" scope', async () => {
22             const { default: defaultExport } =
23              await import(fixtures.fileURL('es-modules/package-type-module/noext-esm'));
24             strictEqual(defaultExport, 'module');
25           });
26
27           it('should import an extensionless JavaScript file within a "type": "module" scope under node_modules',
28              async () => {
29                const { default: defaultExport } =
30                await import(fixtures.fileURL(
31                  'es-modules/package-type-module/node_modules/dep-with-package-json-type-module/noext-esm'));
32                strictEqual(defaultExport, 'module');
33              });
34
35           it('should run as Wasm an extensionless Wasm file within a "type": "module" scope', async () => {
36             const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
37               '--experimental-default-type=module',
38               '--experimental-wasm-modules',
39               '--no-warnings',
40               fixtures.path('es-modules/package-type-module/noext-wasm'),
41             ]);
42
43             strictEqual(stderr, '');
44             strictEqual(stdout, 'executed\n');
45             strictEqual(code, 0);
46             strictEqual(signal, null);
47           });
48
49           it('should import as Wasm an extensionless Wasm file within a "type": "module" scope', async () => {
50             const { add } = await import(fixtures.fileURL('es-modules/package-type-module/noext-wasm'));
51             strictEqual(add(1, 2), 3);
52           });
53
54           it('should import an extensionless Wasm file within a "type": "module" scope under node_modules',
55              async () => {
56                const { add } = await import(fixtures.fileURL(
57                  'es-modules/package-type-module/node_modules/dep-with-package-json-type-module/noext-wasm'));
58                strictEqual(add(1, 2), 3);
59              });
60         });
61
62describe(`the type flag should change the interpretation of certain files within a package scope that lacks a
63"type" field and is not under node_modules`, { concurrency: true }, () => {
64  it('should run as ESM a .js file within package scope that has no defined "type" and is not under node_modules',
65     async () => {
66       const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
67         '--experimental-default-type=module',
68         fixtures.path('es-modules/package-without-type/module.js'),
69       ]);
70
71       strictEqual(stderr, '');
72       strictEqual(stdout, 'executed\n');
73       strictEqual(code, 0);
74       strictEqual(signal, null);
75     });
76
77  it(`should run as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is not
78under node_modules`, async () => {
79    const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
80      '--experimental-default-type=module',
81      fixtures.path('es-modules/package-without-type/noext-esm'),
82    ]);
83
84    strictEqual(stderr, '');
85    strictEqual(stdout, 'executed\n');
86    strictEqual(code, 0);
87    strictEqual(signal, null);
88  });
89
90  it(`should run as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not under
91  node_modules`, async () => {
92    const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
93      '--experimental-default-type=module',
94      '--experimental-wasm-modules',
95      '--no-warnings',
96      fixtures.path('es-modules/noext-wasm'),
97    ]);
98
99    strictEqual(stderr, '');
100    strictEqual(stdout, '');
101    strictEqual(code, 0);
102    strictEqual(signal, null);
103  });
104
105  it('should import as ESM a .js file within package scope that has no defined "type" and is not under node_modules',
106     async () => {
107       const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/module.js'));
108       strictEqual(defaultExport, 'module');
109     });
110
111  it(`should import as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is
112  not under node_modules`, async () => {
113    const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/noext-esm'));
114    strictEqual(defaultExport, 'module');
115  });
116
117  it(`should import as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not
118  under node_modules`, async () => {
119    const { add } = await import(fixtures.fileURL('es-modules/noext-wasm'));
120    strictEqual(add(1, 2), 3);
121  });
122});
123
124describe(`the type flag should NOT change the interpretation of certain files within a package scope that lacks a
125"type" field and is under node_modules`, { concurrency: true }, () => {
126  it('should run as CommonJS a .js file within package scope that has no defined "type" and is under node_modules',
127     async () => {
128       const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
129         '--experimental-default-type=module',
130         fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json-without-type/run.js'),
131       ]);
132
133       strictEqual(stderr, '');
134       strictEqual(stdout, 'executed\n');
135       strictEqual(code, 0);
136       strictEqual(signal, null);
137     });
138
139  it(`should import as CommonJS a .js file within a package scope that has no defined "type" and is under
140  node_modules`, async () => {
141    const { default: defaultExport } =
142      await import(fixtures.fileURL(
143        'es-modules/package-type-module/node_modules/dep-with-package-json-without-type/run.js'));
144    strictEqual(defaultExport, 42);
145  });
146
147  it(`should run as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and is
148  under node_modules`, async () => {
149    const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
150      '--experimental-default-type=module',
151      fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json-without-type/noext-cjs'),
152    ]);
153
154    strictEqual(stderr, '');
155    strictEqual(stdout, 'executed\n');
156    strictEqual(code, 0);
157    strictEqual(signal, null);
158  });
159
160  it(`should import as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and
161  is under node_modules`, async () => {
162    const { default: defaultExport } =
163      await import(fixtures.fileURL(
164        'es-modules/package-type-module/node_modules/dep-with-package-json-without-type/noext-cjs'));
165    strictEqual(defaultExport, 42);
166  });
167});
168