|
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 |
|
@@ -370,18 +371,119 @@ describe('Loader hooks', { concurrency: true }, () => {
|
370 | 371 | });
|
371 | 372 | });
|
372 | 373 |
|
373 |
| - it('should handle globalPreload returning undefined', async () => { |
374 |
| - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
375 |
| - '--no-warnings', |
376 |
| - '--experimental-loader', |
377 |
| - 'data:text/javascript,export function globalPreload(){}', |
378 |
| - fixtures.path('empty.js'), |
379 |
| - ]); |
| 374 | + describe('globalPreload', () => { |
| 375 | + it('should handle globalPreload returning undefined', async () => { |
| 376 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 377 | + '--no-warnings', |
| 378 | + '--experimental-loader', |
| 379 | + 'data:text/javascript,export function globalPreload(){}', |
| 380 | + fixtures.path('empty.js'), |
| 381 | + ]); |
380 | 382 |
|
381 |
| - assert.strictEqual(stderr, ''); |
382 |
| - assert.strictEqual(stdout, ''); |
383 |
| - assert.strictEqual(code, 0); |
384 |
| - assert.strictEqual(signal, null); |
| 383 | + assert.strictEqual(stderr, ''); |
| 384 | + assert.strictEqual(stdout, ''); |
| 385 | + assert.strictEqual(code, 0); |
| 386 | + assert.strictEqual(signal, null); |
| 387 | + }); |
| 388 | + |
| 389 | + it('should handle loading node:test', async () => { |
| 390 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 391 | + '--no-warnings', |
| 392 | + '--experimental-loader', |
| 393 | + 'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}', |
| 394 | + fixtures.path('empty.js'), |
| 395 | + ]); |
| 396 | + |
| 397 | + assert.strictEqual(stderr, ''); |
| 398 | + assert.match(stdout, /\n# pass 1\r?\n/); |
| 399 | + assert.strictEqual(code, 0); |
| 400 | + assert.strictEqual(signal, null); |
| 401 | + }); |
| 402 | + |
| 403 | + it('should handle loading node:os with node: prefix', async () => { |
| 404 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 405 | + '--no-warnings', |
| 406 | + '--experimental-loader', |
| 407 | + 'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}', |
| 408 | + fixtures.path('empty.js'), |
| 409 | + ]); |
| 410 | + |
| 411 | + assert.strictEqual(stderr, ''); |
| 412 | + assert.strictEqual(stdout.trim(), os.arch()); |
| 413 | + assert.strictEqual(code, 0); |
| 414 | + assert.strictEqual(signal, null); |
| 415 | + }); |
| 416 | + |
| 417 | + // `os` is used here because it's simple and not mocked (the builtin module otherwise doesn't matter). |
| 418 | + it('should handle loading builtin module without node: prefix', async () => { |
| 419 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 420 | + '--no-warnings', |
| 421 | + '--experimental-loader', |
| 422 | + 'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}', |
| 423 | + fixtures.path('empty.js'), |
| 424 | + ]); |
| 425 | + |
| 426 | + assert.strictEqual(stderr, ''); |
| 427 | + assert.strictEqual(stdout.trim(), os.arch()); |
| 428 | + assert.strictEqual(code, 0); |
| 429 | + assert.strictEqual(signal, null); |
| 430 | + }); |
| 431 | + |
| 432 | + it('should throw when loading node:test without node: prefix', async () => { |
| 433 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 434 | + '--no-warnings', |
| 435 | + '--experimental-loader', |
| 436 | + 'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}', |
| 437 | + fixtures.path('empty.js'), |
| 438 | + ]); |
| 439 | + |
| 440 | + assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/); |
| 441 | + assert.strictEqual(stdout, ''); |
| 442 | + assert.strictEqual(code, 1); |
| 443 | + assert.strictEqual(signal, null); |
| 444 | + }); |
| 445 | + |
| 446 | + it('should register globals set from globalPreload', async () => { |
| 447 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 448 | + '--no-warnings', |
| 449 | + '--experimental-loader', |
| 450 | + 'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}', |
| 451 | + '--print', 'myGlobal', |
| 452 | + ]); |
| 453 | + |
| 454 | + assert.strictEqual(stderr, ''); |
| 455 | + assert.strictEqual(stdout.trim(), '4'); |
| 456 | + assert.strictEqual(code, 0); |
| 457 | + assert.strictEqual(signal, null); |
| 458 | + }); |
| 459 | + |
| 460 | + it('should log console.log calls returned from globalPreload', async () => { |
| 461 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 462 | + '--no-warnings', |
| 463 | + '--experimental-loader', |
| 464 | + 'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}', |
| 465 | + fixtures.path('empty.js'), |
| 466 | + ]); |
| 467 | + |
| 468 | + assert.strictEqual(stderr, ''); |
| 469 | + assert.strictEqual(stdout.trim(), 'Hello from globalPreload'); |
| 470 | + assert.strictEqual(code, 0); |
| 471 | + assert.strictEqual(signal, null); |
| 472 | + }); |
| 473 | + |
| 474 | + it('should crash if globalPreload returns code that throws', async () => { |
| 475 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 476 | + '--no-warnings', |
| 477 | + '--experimental-loader', |
| 478 | + 'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}', |
| 479 | + fixtures.path('empty.js'), |
| 480 | + ]); |
| 481 | + |
| 482 | + assert.match(stderr, /error from globalPreload/); |
| 483 | + assert.strictEqual(stdout, ''); |
| 484 | + assert.strictEqual(code, 1); |
| 485 | + assert.strictEqual(signal, null); |
| 486 | + }); |
385 | 487 | });
|
386 | 488 |
|
387 | 489 | it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => {
|
|
0 commit comments