|
1 | 1 | import { spawnPromisified } from '../common/index.mjs';
|
2 | 2 | import * as fixtures from '../common/fixtures.mjs';
|
3 | 3 | import assert from 'node:assert';
|
| 4 | +import os from 'node:os'; |
4 | 5 | import { execPath } from 'node:process';
|
5 | 6 | import { describe, it } from 'node:test';
|
6 | 7 |
|
@@ -422,18 +423,119 @@ describe('Loader hooks', { concurrency: true }, () => {
|
422 | 423 | });
|
423 | 424 | });
|
424 | 425 |
|
425 |
| - it('should handle globalPreload returning undefined', async () => { |
426 |
| - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
427 |
| - '--no-warnings', |
428 |
| - '--experimental-loader', |
429 |
| - 'data:text/javascript,export function globalPreload(){}', |
430 |
| - fixtures.path('empty.js'), |
431 |
| - ]); |
| 426 | + describe('globalPreload', () => { |
| 427 | + it('should handle globalPreload returning undefined', async () => { |
| 428 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 429 | + '--no-warnings', |
| 430 | + '--experimental-loader', |
| 431 | + 'data:text/javascript,export function globalPreload(){}', |
| 432 | + fixtures.path('empty.js'), |
| 433 | + ]); |
432 | 434 |
|
433 |
| - assert.strictEqual(stderr, ''); |
434 |
| - assert.strictEqual(stdout, ''); |
435 |
| - assert.strictEqual(code, 0); |
436 |
| - assert.strictEqual(signal, null); |
| 435 | + assert.strictEqual(stderr, ''); |
| 436 | + assert.strictEqual(stdout, ''); |
| 437 | + assert.strictEqual(code, 0); |
| 438 | + assert.strictEqual(signal, null); |
| 439 | + }); |
| 440 | + |
| 441 | + it('should handle loading node:test', async () => { |
| 442 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 443 | + '--no-warnings', |
| 444 | + '--experimental-loader', |
| 445 | + 'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}', |
| 446 | + fixtures.path('empty.js'), |
| 447 | + ]); |
| 448 | + |
| 449 | + assert.strictEqual(stderr, ''); |
| 450 | + assert.match(stdout, /\n# pass 1\r?\n/); |
| 451 | + assert.strictEqual(code, 0); |
| 452 | + assert.strictEqual(signal, null); |
| 453 | + }); |
| 454 | + |
| 455 | + it('should handle loading node:os with node: prefix', async () => { |
| 456 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 457 | + '--no-warnings', |
| 458 | + '--experimental-loader', |
| 459 | + 'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}', |
| 460 | + fixtures.path('empty.js'), |
| 461 | + ]); |
| 462 | + |
| 463 | + assert.strictEqual(stderr, ''); |
| 464 | + assert.strictEqual(stdout.trim(), os.arch()); |
| 465 | + assert.strictEqual(code, 0); |
| 466 | + assert.strictEqual(signal, null); |
| 467 | + }); |
| 468 | + |
| 469 | + // `os` is used here because it's simple and not mocked (the builtin module otherwise doesn't matter). |
| 470 | + it('should handle loading builtin module without node: prefix', async () => { |
| 471 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 472 | + '--no-warnings', |
| 473 | + '--experimental-loader', |
| 474 | + 'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}', |
| 475 | + fixtures.path('empty.js'), |
| 476 | + ]); |
| 477 | + |
| 478 | + assert.strictEqual(stderr, ''); |
| 479 | + assert.strictEqual(stdout.trim(), os.arch()); |
| 480 | + assert.strictEqual(code, 0); |
| 481 | + assert.strictEqual(signal, null); |
| 482 | + }); |
| 483 | + |
| 484 | + it('should throw when loading node:test without node: prefix', async () => { |
| 485 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 486 | + '--no-warnings', |
| 487 | + '--experimental-loader', |
| 488 | + 'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}', |
| 489 | + fixtures.path('empty.js'), |
| 490 | + ]); |
| 491 | + |
| 492 | + assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/); |
| 493 | + assert.strictEqual(stdout, ''); |
| 494 | + assert.strictEqual(code, 1); |
| 495 | + assert.strictEqual(signal, null); |
| 496 | + }); |
| 497 | + |
| 498 | + it('should register globals set from globalPreload', async () => { |
| 499 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 500 | + '--no-warnings', |
| 501 | + '--experimental-loader', |
| 502 | + 'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}', |
| 503 | + '--print', 'myGlobal', |
| 504 | + ]); |
| 505 | + |
| 506 | + assert.strictEqual(stderr, ''); |
| 507 | + assert.strictEqual(stdout.trim(), '4'); |
| 508 | + assert.strictEqual(code, 0); |
| 509 | + assert.strictEqual(signal, null); |
| 510 | + }); |
| 511 | + |
| 512 | + it('should log console.log calls returned from globalPreload', async () => { |
| 513 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 514 | + '--no-warnings', |
| 515 | + '--experimental-loader', |
| 516 | + 'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}', |
| 517 | + fixtures.path('empty.js'), |
| 518 | + ]); |
| 519 | + |
| 520 | + assert.strictEqual(stderr, ''); |
| 521 | + assert.strictEqual(stdout.trim(), 'Hello from globalPreload'); |
| 522 | + assert.strictEqual(code, 0); |
| 523 | + assert.strictEqual(signal, null); |
| 524 | + }); |
| 525 | + |
| 526 | + it('should crash if globalPreload returns code that throws', async () => { |
| 527 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 528 | + '--no-warnings', |
| 529 | + '--experimental-loader', |
| 530 | + 'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}', |
| 531 | + fixtures.path('empty.js'), |
| 532 | + ]); |
| 533 | + |
| 534 | + assert.match(stderr, /error from globalPreload/); |
| 535 | + assert.strictEqual(stdout, ''); |
| 536 | + assert.strictEqual(code, 1); |
| 537 | + assert.strictEqual(signal, null); |
| 538 | + }); |
437 | 539 | });
|
438 | 540 |
|
439 | 541 | it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => {
|
|
0 commit comments