-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
esm-loader.spec.ts
488 lines (458 loc) · 17.4 KB
/
esm-loader.spec.ts
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// ESM loader hook tests
// TODO: at the time of writing, other ESM loader hook tests have not been moved into this file.
// Should consolidate them here.
import { context } from './testlib';
import semver = require('semver');
import {
BIN_ESM_PATH,
BIN_PATH,
BIN_PATH_JS,
CMD_ESM_LOADER_WITHOUT_PROJECT,
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
ctxTsNode,
delay,
EXPERIMENTAL_MODULES_FLAG,
nodeSupportsEsmHooks,
nodeSupportsImportAssertions,
nodeSupportsUnflaggedJsonImports,
nodeSupportsSpawningChildProcess,
nodeUsesNewHooksApi,
resetNodeEnvironment,
TEST_DIR,
tsSupportsImportAssertions,
tsSupportsResolveJsonModule,
} from './helpers';
import { createExec, createSpawn, ExecReturn } from './exec-helpers';
import { join, resolve } from 'path';
import * as expect from 'expect';
import type { NodeLoaderHooksAPI2 } from '../';
import { pathToFileURL } from 'url';
const test = context(ctxTsNode);
const exec = createExec({
cwd: TEST_DIR,
});
const spawn = createSpawn({
cwd: TEST_DIR,
});
test.suite('esm', (test) => {
test.suite('when node supports loader hooks', (test) => {
test.runIf(nodeSupportsEsmHooks);
test('should compile and execute as ESM', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} index.ts`,
{
cwd: join(TEST_DIR, './esm'),
}
);
expect(err).toBe(null);
expect(stdout).toBe('foo bar baz biff libfoo\n');
});
test('should use source maps', async (t) => {
const { err, stdout, stderr } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} "throw error.ts"`,
{
cwd: join(TEST_DIR, './esm'),
}
);
expect(err).not.toBe(null);
const expectedModuleUrl = pathToFileURL(
join(TEST_DIR, './esm/throw error.ts')
).toString();
expect(err!.message).toMatch(
[
`${expectedModuleUrl}:100`,
" bar() { throw new Error('this is a demo'); }",
' ^',
'Error: this is a demo',
` at Foo.bar (${expectedModuleUrl}:100:17)`,
].join('\n')
);
});
test.suite('supports experimental-specifier-resolution=node', (test) => {
test('via --experimental-specifier-resolution', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} --experimental-specifier-resolution=node index.ts`,
{ cwd: join(TEST_DIR, './esm-node-resolver') }
);
expect(err).toBe(null);
expect(stdout).toBe('foo bar baz biff libfoo\n');
});
test('via --es-module-specifier-resolution alias', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ${EXPERIMENTAL_MODULES_FLAG} --es-module-specifier-resolution=node index.ts`,
{ cwd: join(TEST_DIR, './esm-node-resolver') }
);
expect(err).toBe(null);
expect(stdout).toBe('foo bar baz biff libfoo\n');
});
test('via NODE_OPTIONS', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} index.ts`,
{
cwd: join(TEST_DIR, './esm-node-resolver'),
env: {
...process.env,
NODE_OPTIONS: `${EXPERIMENTAL_MODULES_FLAG} --experimental-specifier-resolution=node`,
},
}
);
expect(err).toBe(null);
expect(stdout).toBe('foo bar baz biff libfoo\n');
});
});
test('throws ERR_REQUIRE_ESM when attempting to require() an ESM script when ESM loader is enabled', async () => {
const { err, stderr } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./index.js`,
{
cwd: join(TEST_DIR, './esm-err-require-esm'),
}
);
expect(err).not.toBe(null);
expect(stderr).toMatch(
'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:'
);
});
test('defers to fallback loaders when URL should not be handled by ts-node', async () => {
const { err, stdout, stderr } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} index.mjs`,
{
cwd: join(TEST_DIR, './esm-import-http-url'),
}
);
expect(err).not.toBe(null);
// expect error from node's default resolver
expect(stderr).toMatch(
/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*(?:\n.*){0,2}\n *at defaultResolve/
);
});
test('should bypass import cache when changing search params', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} index.ts`,
{
cwd: join(TEST_DIR, './esm-import-cache'),
}
);
expect(err).toBe(null);
expect(stdout).toBe('log1\nlog2\nlog2\n');
});
test('should support transpile only mode via dedicated loader entrypoint', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT}/transpile-only index.ts`,
{
cwd: join(TEST_DIR, './esm-transpile-only'),
}
);
expect(err).toBe(null);
expect(stdout).toBe('');
});
test('should throw type errors without transpile-only enabled', async () => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} index.ts`,
{
cwd: join(TEST_DIR, './esm-transpile-only'),
}
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch('Unable to compile TypeScript');
expect(err.message).toMatch(
new RegExp(
"TS2345: Argument of type '(?:number|1101)' is not assignable to parameter of type 'string'\\."
)
);
expect(err.message).toMatch(
new RegExp(
"TS2322: Type '(?:\"hello world\"|string)' is not assignable to type 'number'\\."
)
);
expect(stdout).toBe('');
});
test.suite('moduleTypes', (test) => {
suite('with vanilla ts transpilation', 'tsconfig.json');
suite('with third-party-transpiler', 'tsconfig-swc.json');
function suite(name: string, tsconfig: string) {
test.suite(name, (test) => {
test('supports CJS webpack.config.ts in an otherwise ESM project', async (t) => {
// A notable case where you can use ts-node's CommonJS loader, not the ESM loader, in an ESM project:
// when loading a webpack.config.ts or similar config
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --project ./module-types/override-to-cjs/${tsconfig} ./module-types/override-to-cjs/test-webpack-config.cjs`
);
expect(err).toBe(null);
expect(stdout).toBe(``);
});
test('should allow importing CJS in an otherwise ESM project', async (t) => {
await run('override-to-cjs', tsconfig, 'cjs');
if (semver.gte(process.version, '14.13.1'))
await run('override-to-cjs', tsconfig, 'mjs');
});
test('should allow importing ESM in an otherwise CJS project', async (t) => {
await run('override-to-esm', tsconfig, 'cjs');
// Node 14.13.0 has a bug(?) where it checks for ESM-only syntax *before* we transform the code.
if (semver.gte(process.version, '14.13.1'))
await run('override-to-esm', tsconfig, 'mjs');
});
});
}
async function run(project: string, config: string, ext: string) {
const { err, stderr, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./module-types/${project}/test.${ext}`,
{
env: {
...process.env,
TS_NODE_PROJECT: `./module-types/${project}/${config}`,
},
}
);
expect(err).toBe(null);
expect(stdout).toBe(`Failures: 0\n`);
}
});
test.suite('createEsmHooks()', (test) => {
test('should create proper hooks with provided instance', async () => {
const { err } = await exec(
`node ${EXPERIMENTAL_MODULES_FLAG} --loader ./loader.mjs index.ts`,
{
cwd: join(TEST_DIR, './esm-custom-loader'),
}
);
if (err === null) {
throw new Error('Command was expected to fail, but it succeeded.');
}
expect(err.message).toMatch(/TS6133:\s+'unusedVar'/);
});
});
test.suite('unit test hooks', ({ context }) => {
const test = context(async (t) => {
const service = t.context.tsNodeUnderTest.create({
cwd: TEST_DIR,
});
t.teardown(() => {
resetNodeEnvironment();
});
return {
service,
hooks: t.context.tsNodeUnderTest.createEsmHooks(service),
};
});
test.suite('data URIs', (test) => {
test.runIf(nodeUsesNewHooksApi);
test('Correctly determines format of data URIs', async (t) => {
const { hooks } = t.context;
const url = 'data:text/javascript,console.log("hello world");';
const result = await (hooks as NodeLoaderHooksAPI2).load(
url,
{ format: undefined },
async (url, context, _ignored) => {
return { format: context.format!, source: '' };
}
);
expect(result.format).toBe('module');
});
});
});
test.suite('supports import assertions', (test) => {
test.runIf(
nodeSupportsImportAssertions &&
tsSupportsImportAssertions &&
tsSupportsResolveJsonModule
);
const macro = test.macro((flags: string) => async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ${flags} ./importJson.ts`,
{
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
}
);
expect(err).toBe(null);
expect(stdout.trim()).toBe(
'A fuchsia car has 2 seats and the doors are open.\nDone!'
);
});
test.suite(
'when node does not require --experimental-json-modules',
(test) => {
test.runIf(nodeSupportsUnflaggedJsonImports);
test('Can import JSON modules with appropriate assertion', macro, '');
}
);
test.suite('when node requires --experimental-json-modules', (test) => {
test.runIf(!nodeSupportsUnflaggedJsonImports);
test(
'Can import JSON using the appropriate flag and assertion',
macro,
'--experimental-json-modules'
);
});
});
test.suite(
'Entrypoint resolution falls back to CommonJS resolver and format',
(test) => {
test('extensionless entrypoint', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./esm-loader-entrypoint-cjs-fallback/extensionless-entrypoint`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe('Hello world!');
});
test('relies upon CommonJS resolution', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./esm-loader-entrypoint-cjs-fallback/relies-upon-cjs-resolution`
);
expect(err).toBe(null);
expect(stdout.trim()).toBe('Hello world!');
});
test('fails as expected when entrypoint does not exist at all', async (t) => {
const { err, stderr } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./esm-loader-entrypoint-cjs-fallback/does-not-exist`
);
expect(err).toBeDefined();
expect(stderr).toContain(`Cannot find module `);
});
}
);
test.suite('spawns child process', async (test) => {
test.runIf(nodeSupportsSpawningChildProcess);
basic('ts-node-esm executable', () =>
exec(`${BIN_ESM_PATH} ./esm-child-process/via-flag/index.ts foo bar`)
);
basic('ts-node --esm flag', () =>
exec(`${BIN_PATH} --esm ./esm-child-process/via-flag/index.ts foo bar`)
);
basic('ts-node w/tsconfig esm:true', () =>
exec(
`${BIN_PATH} --esm ./esm-child-process/via-tsconfig/index.ts foo bar`
)
);
function basic(title: string, cb: () => ExecReturn) {
test(title, async (t) => {
const { err, stdout, stderr } = await cb();
expect(err).toBe(null);
expect(stdout.trim()).toBe('CLI args: foo bar');
expect(stderr).toBe('');
});
}
test.suite('parent passes signals to child', (test) => {
test.runSerially();
signalTest('SIGINT');
signalTest('SIGTERM');
function signalTest(signal: string) {
test(signal, async (t) => {
const childP = spawn([
// exec lets us run the shims on windows; spawn does not
process.execPath,
BIN_PATH_JS,
`./esm-child-process/via-tsconfig/sleep.ts`,
]);
let code: number | null | undefined = undefined;
childP.child.on('exit', (_code) => (code = _code));
await delay(6e3);
const codeAfter6Seconds = code;
process.kill(childP.child.pid, signal);
await delay(2e3);
const codeAfter8Seconds = code;
const { stdoutP, stderrP } = await childP;
const stdout = await stdoutP;
const stderr = await stderrP;
t.log({
stdout,
stderr,
codeAfter6Seconds,
codeAfter8Seconds,
code,
});
expect(codeAfter6Seconds).toBeUndefined();
if (process.platform === 'win32') {
// Windows doesn't have signals, and node attempts an imperfect facsimile.
// In Windows, SIGINT and SIGTERM kill the process immediately with exit
// code 1, and the process can't catch or prevent this.
expect(codeAfter8Seconds).toBe(1);
expect(code).toBe(1);
} else {
expect(codeAfter8Seconds).toBe(undefined);
expect(code).toBe(123);
expect(stdout.trim()).toBe(
`child registered signal handlers\nchild received signal: ${signal}\nchild exiting`
);
}
expect(stderr).toBe('');
});
}
});
});
});
test.suite('node >= 12.x.x', (test) => {
test.runIf(semver.gte(process.version, '12.0.0'));
test('throws ERR_REQUIRE_ESM when attempting to require() an ESM script when ESM loader is *not* enabled and node version is >= 12', async () => {
// Node versions >= 12 support package.json "type" field and so will throw an error when attempting to load ESM as CJS
const { err, stderr } = await exec(`${BIN_PATH} ./index.js`, {
cwd: join(TEST_DIR, './esm-err-require-esm'),
});
expect(err).not.toBe(null);
expect(stderr).toMatch(
'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:'
);
});
});
test.suite('node < 12.x.x', (test) => {
test.runIf(semver.lt(process.version, '12.0.0'));
test('Loads as CommonJS when attempting to require() an ESM script when ESM loader is *not* enabled and node version is < 12', async () => {
// Node versions less than 12 do not support package.json "type" field and so will load ESM as CommonJS
const { err, stdout } = await exec(`${BIN_PATH} ./index.js`, {
cwd: join(TEST_DIR, './esm-err-require-esm'),
});
expect(err).toBe(null);
expect(stdout).toMatch('CommonJS');
});
});
});
test.suite("Catch unexpected changes to node's loader context", (test) => {
// loader context includes import assertions, therefore this test requires support for import assertions
test.runIf(nodeSupportsImportAssertions);
/*
* This does not test ts-node.
* Rather, it is meant to alert us to potentially breaking changes in node's
* loader API. If node starts returning more or less properties on `context`
* objects, we want to know, because it may indicate that our loader code
* should be updated to accomodate the new properties, either by proxying them,
* modifying them, or suppressing them.
*/
test('Ensure context passed to loader by node has only expected properties', async (t) => {
const { stdout, stderr } = await exec(
`node --loader ./esm-loader-context/loader.mjs --experimental-json-modules ./esm-loader-context/index.mjs`
);
const rows = stdout.split('\n').filter((v) => v[0] === '{');
expect(rows.length).toBe(14);
rows.forEach((row) => {
const json = JSON.parse(row) as {
resolveContextKeys?: string[];
loadContextKeys?: string;
};
if (json.resolveContextKeys) {
expect(json.resolveContextKeys).toEqual([
'conditions',
'importAssertions',
'parentURL',
]);
} else if (json.loadContextKeys) {
try {
expect(json.loadContextKeys).toEqual(['format', 'importAssertions']);
} catch (e) {
// HACK for https://github.com/TypeStrong/ts-node/issues/1641
if (process.version.includes('nightly')) {
expect(json.loadContextKeys).toEqual([
'format',
'importAssertions',
'parentURL',
]);
} else {
throw e;
}
}
} else {
throw new Error('Unexpected stdout in test.');
}
});
});
});