Skip to content

Commit 724b84e

Browse files
test_runner: add env option to run function
Support an `env` option that is passed to the underlying child_process. Fixes: #60709
1 parent e1fc3dc commit 724b84e

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

doc/api/test.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,9 @@ changes:
14111411
- v18.17.0
14121412
pr-url: https://github.com/nodejs/node/pull/47628
14131413
description: Add a testNamePatterns option.
1414+
- version: REPLACEME
1415+
pr-url: https://github.com/nodejs/node/pull/61367
1416+
description: Add the `env` option
14141417
-->
14151418

14161419
* `options` {Object} Configuration options for running tests. The following
@@ -1504,6 +1507,7 @@ changes:
15041507
* `functionCoverage` {number} Require a minimum percent of covered functions. If code
15051508
coverage does not reach the threshold specified, the process will exit with code `1`.
15061509
**Default:** `0`.
1510+
* `env` {Object} Specify environment variables to be passed along to the test process.
15071511
* Returns: {TestsStream}
15081512

15091513
**Note:** `shard` is used to horizontally parallelize test running across

lib/internal/test_runner/runner.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ function runTestFile(path, filesWatcher, opts) {
403403
const subtest = opts.root.createSubtest(FileTest, testPath, testOpts, async (t) => {
404404
const args = getRunArgs(path, opts);
405405
const stdio = ['pipe', 'pipe', 'pipe'];
406-
const env = { __proto__: null, ...process.env, NODE_TEST_CONTEXT: 'child-v8' };
406+
const env = { __proto__: null, ...process.env, ...opts.env, NODE_TEST_CONTEXT: 'child-v8' };
407407
if (watchMode) {
408408
stdio.push('ipc');
409409
env.WATCH_REPORT_DEPENDENCIES = '1';
@@ -610,6 +610,7 @@ function run(options = kEmptyObject) {
610610
argv = [],
611611
cwd = process.cwd(),
612612
rerunFailuresFilePath,
613+
env,
613614
} = options;
614615

615616
if (files != null) {
@@ -718,6 +719,10 @@ function run(options = kEmptyObject) {
718719
validatePath(globalSetupPath, 'options.globalSetupPath');
719720
}
720721

722+
if (env != null) {
723+
validateObject(env);
724+
}
725+
721726
const rootTestOptions = { __proto__: null, concurrency, timeout, signal };
722727
const globalOptions = {
723728
__proto__: null,
@@ -763,6 +768,7 @@ function run(options = kEmptyObject) {
763768
argv,
764769
execArgv,
765770
rerunFailuresFilePath,
771+
env,
766772
};
767773

768774
if (isolation === 'process') {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { test } = require('node:test');
2+
3+
test('process.env is correct', (t) => {
4+
t.assert.strictEqual(process.env.FOOBAR, 'FUZZBUZZ');
5+
});

test/parallel/test-runner-run.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,14 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
648648
assert.strictEqual(diagnostics.includes(entry), true);
649649
}
650650
});
651+
652+
it('should allow env variables to be configured', async () => {
653+
const stream = run({ files: [join(testFixtures, 'process-env.js')], env: { FOOBAR: 'FUZZBUZZ' } });
654+
stream.on('test:fail', common.mustNotCall());
655+
stream.on('test:pass', common.mustCall(1));
656+
// eslint-disable-next-line no-unused-vars
657+
for await (const _ of stream);
658+
});
651659
});
652660

653661
describe('forceExit', () => {

0 commit comments

Comments
 (0)