|
| 1 | +import { spawnPromisified } from '../common/index.mjs'; |
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { execPath } from 'node:process'; |
| 5 | +import { describe, it } from 'node:test'; |
| 6 | + |
| 7 | +describe('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 | + // The worker code should always run before the --import, but the console.log might arrive late. |
| 52 | + assert.match(stdout, /^A\r?\nA\r?\n(B\r?\nC|C\r?\nB)\r?\nD\r?\n$/); |
| 53 | + assert.strictEqual(code, 0); |
| 54 | + assert.strictEqual(signal, null); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should support --require and --import along with using a loader written in ESM and ESM entry point', async () => { |
| 58 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 59 | + '--no-warnings', |
| 60 | + '--require', |
| 61 | + fixtures.path('printA.js'), |
| 62 | + '--experimental-loader', |
| 63 | + 'data:text/javascript,console.log("B")', |
| 64 | + '--import', |
| 65 | + fixtures.fileURL('printC.js'), |
| 66 | + '--input-type=module', |
| 67 | + '--eval', |
| 68 | + 'setTimeout(() => console.log("D"),99)', |
| 69 | + ]); |
| 70 | + |
| 71 | + assert.strictEqual(stderr, ''); |
| 72 | + // The worker code should always run before the --import, but the console.log might arrive late. |
| 73 | + assert.match(stdout, /^A\r?\nA\r?\n(B\r?\nC|C\r?\nB)\r?\nD\r?\n$/); |
| 74 | + assert.strictEqual(code, 0); |
| 75 | + assert.strictEqual(signal, null); |
| 76 | + }); |
| 77 | +}); |
0 commit comments