|
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,60 +423,118 @@ 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); |
437 |
| - }); |
| 435 | + assert.strictEqual(stderr, ''); |
| 436 | + assert.strictEqual(stdout, ''); |
| 437 | + assert.strictEqual(code, 0); |
| 438 | + assert.strictEqual(signal, null); |
| 439 | + }); |
438 | 440 |
|
439 |
| - it('should register globals set from globalPreload', async () => { |
440 |
| - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
441 |
| - '--no-warnings', |
442 |
| - '--experimental-loader', |
443 |
| - 'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}', |
444 |
| - '--print', 'myGlobal', |
445 |
| - ]); |
| 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 | + ]); |
446 | 448 |
|
447 |
| - assert.strictEqual(stderr, ''); |
448 |
| - assert.strictEqual(stdout.trim(), '4'); |
449 |
| - assert.strictEqual(code, 0); |
450 |
| - assert.strictEqual(signal, null); |
451 |
| - }); |
| 449 | + assert.strictEqual(stderr, ''); |
| 450 | + assert.match(stdout, /\n# pass 1\r?\n/); |
| 451 | + assert.strictEqual(code, 0); |
| 452 | + assert.strictEqual(signal, null); |
| 453 | + }); |
452 | 454 |
|
453 |
| - it('should log console.log calls returned from globalPreload', async () => { |
454 |
| - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
455 |
| - '--no-warnings', |
456 |
| - '--experimental-loader', |
457 |
| - 'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}', |
458 |
| - fixtures.path('empty.js'), |
459 |
| - ]); |
| 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 | + ]); |
460 | 462 |
|
461 |
| - assert.strictEqual(stderr, ''); |
462 |
| - assert.strictEqual(stdout.trim(), 'Hello from globalPreload'); |
463 |
| - assert.strictEqual(code, 0); |
464 |
| - assert.strictEqual(signal, null); |
465 |
| - }); |
| 463 | + assert.strictEqual(stderr, ''); |
| 464 | + assert.strictEqual(stdout.trim(), os.arch()); |
| 465 | + assert.strictEqual(code, 0); |
| 466 | + assert.strictEqual(signal, null); |
| 467 | + }); |
466 | 468 |
|
467 |
| - it('should crash if globalPreload returns code that throws', async () => { |
468 |
| - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
469 |
| - '--no-warnings', |
470 |
| - '--experimental-loader', |
471 |
| - 'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}', |
472 |
| - fixtures.path('empty.js'), |
473 |
| - ]); |
| 469 | + it('should handle loading node:os without node: prefix', async () => { |
| 470 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 471 | + '--no-warnings', |
| 472 | + '--experimental-loader', |
| 473 | + 'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}', |
| 474 | + fixtures.path('empty.js'), |
| 475 | + ]); |
474 | 476 |
|
475 |
| - assert.match(stderr, /error from globalPreload/); |
476 |
| - assert.strictEqual(stdout, ''); |
477 |
| - assert.strictEqual(code, 1); |
478 |
| - assert.strictEqual(signal, null); |
| 477 | + assert.strictEqual(stderr, ''); |
| 478 | + assert.strictEqual(stdout.trim(), os.arch()); |
| 479 | + assert.strictEqual(code, 0); |
| 480 | + assert.strictEqual(signal, null); |
| 481 | + }); |
| 482 | + |
| 483 | + it('should throw when loading node:test without node: prefix', async () => { |
| 484 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 485 | + '--no-warnings', |
| 486 | + '--experimental-loader', |
| 487 | + 'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}', |
| 488 | + fixtures.path('empty.js'), |
| 489 | + ]); |
| 490 | + |
| 491 | + assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/); |
| 492 | + assert.strictEqual(stdout, ''); |
| 493 | + assert.strictEqual(code, 1); |
| 494 | + assert.strictEqual(signal, null); |
| 495 | + }); |
| 496 | + |
| 497 | + it('should register globals set from globalPreload', async () => { |
| 498 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 499 | + '--no-warnings', |
| 500 | + '--experimental-loader', |
| 501 | + 'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}', |
| 502 | + '--print', 'myGlobal', |
| 503 | + ]); |
| 504 | + |
| 505 | + assert.strictEqual(stderr, ''); |
| 506 | + assert.strictEqual(stdout.trim(), '4'); |
| 507 | + assert.strictEqual(code, 0); |
| 508 | + assert.strictEqual(signal, null); |
| 509 | + }); |
| 510 | + |
| 511 | + it('should log console.log calls returned from globalPreload', async () => { |
| 512 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 513 | + '--no-warnings', |
| 514 | + '--experimental-loader', |
| 515 | + 'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}', |
| 516 | + fixtures.path('empty.js'), |
| 517 | + ]); |
| 518 | + |
| 519 | + assert.strictEqual(stderr, ''); |
| 520 | + assert.strictEqual(stdout.trim(), 'Hello from globalPreload'); |
| 521 | + assert.strictEqual(code, 0); |
| 522 | + assert.strictEqual(signal, null); |
| 523 | + }); |
| 524 | + |
| 525 | + it('should crash if globalPreload returns code that throws', async () => { |
| 526 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 527 | + '--no-warnings', |
| 528 | + '--experimental-loader', |
| 529 | + 'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}', |
| 530 | + fixtures.path('empty.js'), |
| 531 | + ]); |
| 532 | + |
| 533 | + assert.match(stderr, /error from globalPreload/); |
| 534 | + assert.strictEqual(stdout, ''); |
| 535 | + assert.strictEqual(code, 1); |
| 536 | + assert.strictEqual(signal, null); |
| 537 | + }); |
479 | 538 | });
|
480 | 539 |
|
481 | 540 | it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => {
|
|
0 commit comments