Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5fe2582

Browse filesBrowse 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 6993386 commit 5fe2582
Copy full SHA for 5fe2582

4 files changed

+47-1Lines changed: 47 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/test.md‎

Copy file name to clipboardExpand all lines: doc/api/test.md
+6Lines changed: 6 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,9 @@ added:
14351435
- v18.9.0
14361436
- v16.19.0
14371437
changes:
1438+
- version: REPLACEME
1439+
pr-url: https://github.com/nodejs/node/pull/61367
1440+
description: Add the `env` option.
14381441
- version: v24.7.0
14391442
pr-url: https://github.com/nodejs/node/pull/59443
14401443
description: Added a rerunFailuresFilePath option.
@@ -1555,6 +1558,9 @@ changes:
15551558
* `functionCoverage` {number} Require a minimum percent of covered functions. If code
15561559
coverage does not reach the threshold specified, the process will exit with code `1`.
15571560
**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`.
15581564
* Returns: {TestsStream}
15591565

15601566
**Note:** `shard` is used to horizontally parallelize test running across
Collapse file

‎lib/internal/test_runner/runner.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js
+11-1Lines 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') {
Collapse file
+7Lines 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+
});
Collapse file

‎test/parallel/test-runner-run.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-run.mjs
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,29 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
650650
});
651651
});
652652

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+
653676
describe('forceExit', () => {
654677
it('throws for non-boolean values', () => {
655678
[Symbol(), {}, 0, 1, '1', Promise.resolve([])].forEach((forceExit) => {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.