test_runner: add env option to run function · nodejs/node@972f824 · GitHub
Skip to content

Commit 972f824

Browse files
Ethan-Arrowoodaduh95
authored andcommitted
test_runner: add env option to run function
Support an `env` option that is passed to the underlying child_process. Fixes: #60709 PR-URL: #61367 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 8e8f705 commit 972f824

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

doc/api/test.md

Lines changed: 6 additions & 0 deletions

lib/internal/test_runner/runner.js

Lines changed: 11 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, NODE_TEST_CONTEXT: 'child-v8', ...(opts.env || process.env) };
407407
if (watchMode) {
408408
stdio.push('ipc');
409409
env.WATCH_REPORT_DEPENDENCIES = '1';
@@ -623,6 +623,7 @@ function run(options = kEmptyObject) {
623623
argv = [],
624624
cwd = process.cwd(),
625625
rerunFailuresFilePath,
626+
env,
626627
} = options;
627628

628629
if (files != null) {
@@ -731,6 +732,14 @@ function run(options = kEmptyObject) {
731732
validatePath(globalSetupPath, 'options.globalSetupPath');
732733
}
733734

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+
734743
const rootTestOptions = { __proto__: null, concurrency, timeout, signal };
735744
const globalOptions = {
736745
__proto__: null,
@@ -776,6 +785,7 @@ function run(options = kEmptyObject) {
776785
argv,
777786
execArgv,
778787
rerunFailuresFilePath,
788+
env,
779789
};
780790

781791
if (isolation === 'process') {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
});

test/parallel/test-runner-run.mjs

Lines changed: 23 additions & 0 deletions

0 commit comments

Comments
 (0)