• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { spawnPromisified } from '../common/index.mjs';
2import * as fixtures from '../common/fixtures.mjs';
3import assert from 'node:assert';
4import { execPath } from 'node:process';
5import { describe, it } from 'node:test';
6
7describe('Worker threads do not spawn infinitely', { concurrency: true }, () => {
8  it('should not trigger an infinite loop when using a loader exports no recognized hooks', async () => {
9    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
10      '--no-warnings',
11      '--experimental-loader',
12      fixtures.fileURL('empty.js'),
13      '--eval',
14      'setTimeout(() => console.log("hello"),99)',
15    ]);
16
17    assert.strictEqual(stderr, '');
18    assert.match(stdout, /^hello\r?\n$/);
19    assert.strictEqual(code, 0);
20    assert.strictEqual(signal, null);
21  });
22
23  it('should support a CommonJS entry point and a loader that imports a CommonJS module', async () => {
24    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
25      '--no-warnings',
26      '--experimental-loader',
27      fixtures.fileURL('es-module-loaders/loader-with-dep.mjs'),
28      fixtures.path('print-delayed.js'),
29    ]);
30
31    assert.strictEqual(stderr, '');
32    assert.match(stdout, /^delayed\r?\n$/);
33    assert.strictEqual(code, 0);
34    assert.strictEqual(signal, null);
35  });
36
37  it('should support --require and --import along with using a loader written in CJS and CJS entry point', async () => {
38    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
39      '--no-warnings',
40      '--eval',
41      'setTimeout(() => console.log("D"),99)',
42      '--import',
43      fixtures.fileURL('printC.js'),
44      '--experimental-loader',
45      fixtures.fileURL('printB.js'),
46      '--require',
47      fixtures.path('printA.js'),
48    ]);
49
50    assert.strictEqual(stderr, '');
51    // We are validating that:
52    // 1. the `--require` flag is run first from the main thread (and A is printed).
53    // 2. the `--require` flag is then run on the loader thread (and A is printed).
54    // 3. the `--loader` module is executed (and B is printed).
55    // 4. the `--import` module is evaluated once, on the main thread (and C is printed).
56    // 5. the user code is finally executed (and D is printed).
57    // The worker code should always run before the --import, but the console.log might arrive late.
58    assert.match(stdout, /^A\r?\n(A\r?\nB\r?\nC|A\r?\nC\r?\nB|C\r?\nA\r?\nB)\r?\nD\r?\n$/);
59    assert.strictEqual(code, 0);
60    assert.strictEqual(signal, null);
61  });
62
63  it('should support --require and --import along with using a loader written in ESM and ESM entry point', async () => {
64    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
65      '--no-warnings',
66      '--require',
67      fixtures.path('printA.js'),
68      '--experimental-loader',
69      'data:text/javascript,console.log("B")',
70      '--import',
71      fixtures.fileURL('printC.js'),
72      '--input-type=module',
73      '--eval',
74      'setTimeout(() => console.log("D"),99)',
75    ]);
76
77    assert.strictEqual(stderr, '');
78    // The worker code should always run before the --import, but the console.log might arrive late.
79    assert.match(stdout, /^A\r?\nA\r?\n(B\r?\nC|C\r?\nB)\r?\nD\r?\n$/);
80    assert.strictEqual(code, 0);
81    assert.strictEqual(signal, null);
82  });
83});
84