• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { spawnPromisified } from '../common/index.mjs';
2import fixtures from '../common/fixtures.js';
3import assert from 'node:assert';
4import { execPath } from 'node:process';
5import { describe, it } from 'node:test';
6
7const cjsEntry = fixtures.path('es-modules', 'cjs-file.cjs');
8const cjsImport = fixtures.fileURL('es-modules', 'cjs-file.cjs');
9const mjsEntry = fixtures.path('es-modules', 'mjs-file.mjs');
10const mjsImport = fixtures.fileURL('es-modules', 'mjs-file.mjs');
11
12
13describe('import modules using --import', { concurrency: true }, () => {
14  it('should import when using --eval', async () => {
15    const { code, signal, stderr, stdout } = await spawnPromisified(
16      execPath,
17      [
18        '--import', mjsImport,
19        '--eval', 'console.log("log")',
20      ]
21    );
22
23    assert.strictEqual(stderr, '');
24    assert.match(stdout, /^\.mjs file\r?\nlog\r?\n$/);
25    assert.strictEqual(code, 0);
26    assert.strictEqual(signal, null);
27  });
28
29  it('should import when using --eval and --input-type=module', async () => {
30    const { code, signal, stderr, stdout } = await spawnPromisified(
31      execPath,
32      [
33        '--import', mjsImport,
34        '--input-type', 'module',
35        '--eval', 'console.log("log")',
36      ]
37    );
38
39    assert.strictEqual(stderr, '');
40    assert.match(stdout, /^\.mjs file\r?\nlog\r?\n$/);
41    assert.strictEqual(code, 0);
42    assert.strictEqual(signal, null);
43  });
44
45  it('should import when main entrypoint is a cjs file', async () => {
46    const { code, signal, stderr, stdout } = await spawnPromisified(
47      execPath,
48      [
49        '--import', mjsImport,
50        cjsEntry,
51      ]
52    );
53
54    assert.strictEqual(stderr, '');
55    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
56    assert.strictEqual(code, 0);
57    assert.strictEqual(signal, null);
58  });
59
60  it('should import mjs when entrypoint is a cjs file', async () => {
61    const { code, signal, stderr, stdout } = await spawnPromisified(
62      execPath,
63      [
64        '--import', mjsImport,
65        cjsEntry,
66      ]
67    );
68
69    assert.strictEqual(stderr, '');
70    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
71    assert.strictEqual(code, 0);
72    assert.strictEqual(signal, null);
73  });
74
75  it('should import cjs when entrypoint is a mjs file', async () => {
76    const { code, signal, stderr, stdout } = await spawnPromisified(
77      execPath,
78      [
79        '--import', cjsImport,
80        mjsEntry,
81      ]
82    );
83
84    assert.strictEqual(stderr, '');
85    assert.match(stdout, /^\.cjs file\r?\n\.mjs file\r?\n$/);
86    assert.strictEqual(code, 0);
87    assert.strictEqual(signal, null);
88  });
89
90  it('should import mjs when entrypoint is a cjs file', async () => {
91    const { code, signal, stderr, stdout } = await spawnPromisified(
92      execPath,
93      [
94        '--import', mjsImport,
95        cjsEntry,
96      ]
97    );
98
99    assert.strictEqual(stderr, '');
100    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
101    assert.strictEqual(code, 0);
102    assert.strictEqual(signal, null);
103  });
104
105  it('should de-duplicate redundant `--import`s', async () => {
106    const { code, signal, stderr, stdout } = await spawnPromisified(
107      execPath,
108      [
109        '--import', mjsImport,
110        '--import', mjsImport,
111        '--import', mjsImport,
112        cjsEntry,
113      ]
114    );
115
116    assert.strictEqual(stderr, '');
117    assert.match(stdout, /^\.mjs file\r?\n\.cjs file\r?\n$/);
118    assert.strictEqual(code, 0);
119    assert.strictEqual(signal, null);
120  });
121
122  it('should import when running --check', async () => {
123    const { code, signal, stderr, stdout } = await spawnPromisified(
124      execPath,
125      [
126        '--import', mjsImport,
127        '--check',
128        cjsEntry,
129      ]
130    );
131
132    assert.strictEqual(stderr, '');
133    assert.match(stdout, /^\.mjs file\r?\n$/);
134    assert.strictEqual(code, 0);
135    assert.strictEqual(signal, null);
136  });
137
138  it('should import when running --check fails', async () => {
139    const { code, signal, stderr, stdout } = await spawnPromisified(
140      execPath,
141      [
142        '--import', mjsImport,
143        '--no-warnings',
144        '--check',
145        fixtures.path('es-modules', 'invalid-cjs.js'),
146      ]
147    );
148
149    assert.match(stderr, /SyntaxError: Unexpected token 'export'/);
150    assert.match(stdout, /^\.mjs file\r?\n$/);
151    assert.strictEqual(code, 1);
152    assert.strictEqual(signal, null);
153  });
154
155  it('should import --require before --import', async () => {
156    const { code, signal, stderr, stdout } = await spawnPromisified(
157      execPath,
158      [
159        '--import', mjsImport,
160        '--require', cjsEntry,
161        '--eval', 'console.log("log")',
162      ]
163    );
164
165    assert.strictEqual(stderr, '');
166    assert.match(stdout, /^\.cjs file\r?\n\.mjs file\r?\nlog\r?\n$/);
167    assert.strictEqual(code, 0);
168    assert.strictEqual(signal, null);
169  });
170
171  it('should import a module with top level await', async () => {
172    const { code, signal, stderr, stdout } = await spawnPromisified(
173      execPath,
174      [
175        '--import', fixtures.fileURL('es-modules', 'esm-top-level-await.mjs'),
176        fixtures.path('es-modules', 'print-3.mjs'),
177      ]
178    );
179
180    assert.strictEqual(stderr, '');
181    assert.match(stdout, /^1\r?\n2\r?\n3\r?\n$/);
182    assert.strictEqual(code, 0);
183    assert.strictEqual(signal, null);
184  });
185});
186