| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6993386 commit 5fe2582
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1435,6 +1435,9 @@ added: | |||
| 1435 | 1435 | - v18.9.0 | |
| 1436 | 1436 | - v16.19.0 | |
| 1437 | 1437 | changes: | |
| 1438 | + - version: REPLACEME | ||
| 1439 | + pr-url: https://github.com/nodejs/node/pull/61367 | ||
| 1440 | + description: Add the `env` option. | ||
| 1438 | 1441 | - version: v24.7.0 | |
| 1439 | 1442 | pr-url: https://github.com/nodejs/node/pull/59443 | |
| 1440 | 1443 | description: Added a rerunFailuresFilePath option. | |
@@ -1555,6 +1558,9 @@ changes: | |||
| 1555 | 1558 | * `functionCoverage` {number} Require a minimum percent of covered functions. If code | |
| 1556 | 1559 | coverage does not reach the threshold specified, the process will exit with code `1`. | |
| 1557 | 1560 | **Default:** `0`. | |
| 1561 | + * `env` {Object} Specify environment variables to be passed along to the test process. | ||
| 1562 | + This options is not compatible with `isolation='none'`. These variables will override | ||
| 1563 | + those from the main process, and are not merged with `process.env`. | ||
| 1558 | 1564 | * Returns: {TestsStream} | |
| 1559 | 1565 | ||
| 1560 | 1566 | **Note:** `shard` is used to horizontally parallelize test running across | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -403,7 +403,7 @@ function runTestFile(path, filesWatcher, opts) { | |||
| 403 | 403 | const subtest = opts.root.createSubtest(FileTest, testPath, testOpts, async (t) => { | |
| 404 | 404 | const args = getRunArgs(path, opts); | |
| 405 | 405 | const stdio = ['pipe', 'pipe', 'pipe']; | |
| 406 | - const env = { __proto__: null, ...process.env, NODE_TEST_CONTEXT: 'child-v8' }; | ||
| 406 | + const env = { __proto__: null, NODE_TEST_CONTEXT: 'child-v8', ...(opts.env || process.env) }; | ||
| 407 | 407 | if (watchMode) { | |
| 408 | 408 | stdio.push('ipc'); | |
| 409 | 409 | env.WATCH_REPORT_DEPENDENCIES = '1'; | |
@@ -623,6 +623,7 @@ function run(options = kEmptyObject) { | |||
| 623 | 623 | argv = [], | |
| 624 | 624 | cwd = process.cwd(), | |
| 625 | 625 | rerunFailuresFilePath, | |
| 626 | + env, | ||
| 626 | 627 | } = options; | |
| 627 | 628 | ||
| 628 | 629 | if (files != null) { | |
@@ -731,6 +732,14 @@ function run(options = kEmptyObject) { | |||
| 731 | 732 | validatePath(globalSetupPath, 'options.globalSetupPath'); | |
| 732 | 733 | } | |
| 733 | 734 | ||
| 735 | + if (env != null) { | ||
| 736 | + validateObject(env); | ||
| 737 | + | ||
| 738 | + if (isolation === 'none') { | ||
| 739 | + throw new ERR_INVALID_ARG_VALUE('options.env', env, 'is not supported with isolation=\'none\''); | ||
| 740 | + } | ||
| 741 | + } | ||
| 742 | + | ||
| 734 | 743 | const rootTestOptions = { __proto__: null, concurrency, timeout, signal }; | |
| 735 | 744 | const globalOptions = { | |
| 736 | 745 | __proto__: null, | |
@@ -776,6 +785,7 @@ function run(options = kEmptyObject) { | |||
| 776 | 785 | argv, | |
| 777 | 786 | execArgv, | |
| 778 | 787 | rerunFailuresFilePath, | |
| 788 | + env, | ||
| 779 | 789 | }; | |
| 780 | 790 | ||
| 781 | 791 | if (isolation === 'process') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + const { test } = require('node:test'); | ||
| 2 | + | ||
| 3 | + test('process.env is correct', (t) => { | ||
| 4 | + t.assert.strictEqual(process.env.ABC, undefined, 'main process env var should be undefined'); | ||
| 5 | + t.assert.strictEqual(process.env.NODE_TEST_CONTEXT, 'child-v8', 'NODE_TEST_CONTEXT should be set by run()'); | ||
| 6 | + t.assert.strictEqual(process.env.FOOBAR, 'FUZZBUZZ', 'specified env var should be defined'); | ||
| 7 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -650,6 +650,29 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { | |||
| 650 | 650 | }); | |
| 651 | 651 | }); | |
| 652 | 652 | ||
| 653 | + describe('env', () => { | ||
| 654 | + it('should allow env variables to be configured', async () => { | ||
| 655 | + // Need to inherit some process.env variables so it runs reliably across different environments. | ||
| 656 | + const env = { ...process.env, FOOBAR: 'FUZZBUZZ' }; | ||
| 657 | + // Set a variable on main process env and test it does not exist within test env. | ||
| 658 | + process.env.ABC = 'XYZ'; | ||
| 659 | + | ||
| 660 | + const stream = run({ files: [join(testFixtures, 'process-env.js')], env }); | ||
| 661 | + stream.on('test:fail', common.mustNotCall()); | ||
| 662 | + stream.on('test:pass', common.mustCall(1)); | ||
| 663 | + // eslint-disable-next-line no-unused-vars | ||
| 664 | + for await (const _ of stream); | ||
| 665 | + delete process.env.ABC; | ||
| 666 | + }); | ||
| 667 | + | ||
| 668 | + it('should throw error when env is specified with isolation=none', async () => { | ||
| 669 | + assert.throws(() => run({ env: { foo: 'bar' }, isolation: 'none' }), { | ||
| 670 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 671 | + message: /The property 'options\.env' is not supported with isolation='none'\. Received { foo: 'bar' }/ | ||
| 672 | + }); | ||
| 673 | + }); | ||
| 674 | + }); | ||
| 675 | + | ||
| 653 | 676 | describe('forceExit', () => { | |
| 654 | 677 | it('throws for non-boolean values', () => { | |
| 655 | 678 | [Symbol(), {}, 0, 1, '1', Promise.resolve([])].forEach((forceExit) => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments