Repository: nightwatchjs/nightwatch
Pull request #3691 (2023-04-12 09:12:26)

fix: env is not passed to worker threads
Fixes: https://github.com/nightwatchjs/nightwatch/issues/3686
Fixes: https://github.com/nightwatchjs/nightwatch/issues/3687
Thanks in advance for your contribution. Please follow the below steps in submitting a pull request, as it will help us with reviewing it quicker.

- [ ] Create a new branch from master (e.g. `features/my-new-feature` or `issue/123-my-bugfix`);
- [ ] If you're fixing a bug also create an issue if one doesn't exist yet;
- [ ] If it's a new feature explain why do you think it's necessary. Please check with the maintainers beforehand to make sure it is something that we will accept. Usually we only accept new features if we feel that they will benefit the entire community;
- [ ] Please avoid sending PRs which contain drastic or low level changes. If you are certain that the changes are needed, please discuss them beforehand and indicate what the impact will be;
- [ ] If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
- [ ] Do not include changes that are not related to the issue at hand;
- [ ] Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
- [ ] Always add unit tests - PRs without tests are most of the times ignored.


gravityvi: Looks good. Should we add a test reflecting envs variables are passed onto worker threads?

Patch:
diff --git a/lib/runner/concurrency/worker-process.js b/lib/runner/concurrency/worker-process.js
index a890a743..bb6bc9e1 100644
--- a/lib/runner/concurrency/worker-process.js
+++ b/lib/runner/concurrency/worker-process.js
@@ -30,6 +30,7 @@ class WorkerPool extends EventEmitter {
       maxThreads: maxWorkerCount,
       argv: args,
       env: {
+        ...process.env,
         __NIGHTWATCH_PARALLEL_MODE: '1'
       }
     });


Test patch:
diff --git a/test/src/runner/cli/testCliRunnerParallel.js b/test/src/runner/cli/testCliRunnerParallel.js
index 91d1ed0e..e767cbc5 100644
--- a/test/src/runner/cli/testCliRunnerParallel.js
+++ b/test/src/runner/cli/testCliRunnerParallel.js
@@ -19,11 +19,11 @@ describe('Test CLI Runner in Parallel', function () {
     });
   }
 
-  before(function() {
+  beforeEach(function() {
     mockery.enable({useCleanCache: true, warnOnReplace: false, warnOnUnregistered: false});
   });
 
-  after(function() {
+  afterEach(function() {
     mockery.deregisterAll();
     mockery.resetCache();
     mockery.disable();
@@ -66,6 +66,7 @@ describe('Test CLI Runner in Parallel', function () {
   });
   
   it('test run geckodriver with concurrency - worker threads', function () {
+    process.env.TEST_ENV = 'TEST';
     class RunnerBaseMock extends RunnerBase {
       static readTestSource(settings, argv) {
         assert.strictEqual(settings.testWorkersEnabled, true);
@@ -80,6 +81,11 @@ describe('Test CLI Runner in Parallel', function () {
     class WorkerProcessMock extends WorkerProcess {
       addTask({colors}) {
         assert.strictEqual(colors.length, 4);
+        assert.strictEqual(this.piscina.options.env.TEST_ENV, 'TEST');
+        assert(Object.keys(this.piscina.options.env).length > 1, 'process.env should have more than one key');
+
+        // cleaning the env
+        process.env.TEST_ENV = undefined;
 
         return Promise.resolve(0);
       }