Repository: nightwatchjs/nightwatch
Pull request #2718 (2021-05-26 11:05:15)

recursively append properties from super-env in test-settings
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.

- [x] Create a new branch from master (e.g. `features/my-new-feature` or `issue/123-my-bugfix`);
- [x] If you're fixing a bug also create an issue if one doesn't exist yet;
- [x] 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;
- [x] 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;
- [x] If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
- [x] Do not include changes that are not related to the issue at hand;
- [x] Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
- [x] Always add unit tests - PRs without tests are most of the times ignored.


gravityvi: Example of the feature:
```
test_setting: {

 browserstack: {
        desiredCapabilities: {
          'bstack:options': {
            local: 'false'
          }
        },
 }

 'browserstack.chrome': {
        extends: 'browserstack',
        desiredCapabilities: {
          browserName: 'chrome',
          chromeOptions: {
            w3c: false
          }
        }
 },
 
 'browserstack.chrome_mac': {
        extends: 'browserstack.chrome',
        desiredCapabilities: {
          os: 'OS X'
        }
  }
}
```

**TEST_ENV='browserstack.chrome_mac'**
**OUTPUT**:
```
test_setting:{
 'browserstack.chrome_mac': {
        desiredCapabilities: {
          os: 'OS X',
          browserName: 'chrome',
          chromeOptions: {
            w3c: false
          },
          'bstack:options': {
            local: 'false'
          }
        }
  }

 }

}

```




Patch:
diff --git a/lib/settings/settings.js b/lib/settings/settings.js
index 42bc4fc1..fedb40aa 100644
--- a/lib/settings/settings.js
+++ b/lib/settings/settings.js
@@ -177,17 +177,24 @@ class Settings {
     }
 
     const testEnvSettings = this.baseSettings.test_settings[this.testEnv] || {};
-    if (testEnvSettings.extends) {
+    this.inheritFromSuperEnv(testEnvSettings);
+    defaultsDeep(testEnvSettings, defaultEnvSettings);
+    lodashMerge(this.settings, testEnvSettings);
+   
+
+    return this;
+  }
+
+  inheritFromSuperEnv(testEnvSettings){
+    if (testEnvSettings.extends) { 
       const superEnv = this.baseSettings.test_settings[testEnvSettings.extends] || {};
-      defaultsDeep(superEnv, defaultEnvSettings);
+      delete testEnvSettings.extends;
       defaultsDeep(testEnvSettings, superEnv);
-    } else {
-      defaultsDeep(testEnvSettings, defaultEnvSettings);
-    }
 
-    lodashMerge(this.settings, testEnvSettings);
+      return this.inheritFromSuperEnv(testEnvSettings);
+    }
 
-    return this;
+    return testEnvSettings;
   }
 
   /**


Test patch:
diff --git a/test/src/index/testSettings.js b/test/src/index/testSettings.js
index 33a68556..a387920d 100644
--- a/test/src/index/testSettings.js
+++ b/test/src/index/testSettings.js
@@ -211,3 +211,58 @@ describe('test Settings', function () {
     assert.strictEqual(settings.testWorkersEnabled, true);
   });
 });
+
+it('recursive extends in test_settings', function () {
+
+  const baseSettings = {
+    test_settings: {
+      browserstack: {
+        selenium: {
+          host: 'hub-cloud.browserstack.com',
+          port: 443
+        },
+        desiredCapabilities: {
+          'bstack:options': {
+            local: 'false'
+          }
+        },
+  
+        disable_error_log: true,
+        webdriver: {
+          keep_alive: true,
+          start_process: false
+        }
+      },
+      'browserstack.chrome': {
+        extends: 'browserstack',
+        desiredCapabilities: {
+          browserName: 'chrome',
+          chromeOptions: {
+            w3c: false
+          }
+        }
+      },
+      'browserstack.chrome_mac': {
+        extends: 'browserstack.chrome',
+        desiredCapabilities: {
+          os: 'OS X'
+        }
+      }
+    }
+  };
+  const expectedDesiredCapabilites = {
+    os: 'OS X',
+    browserName: 'chrome',
+    chromeOptions: {
+      w3c: false
+    },
+    'bstack:options': {
+      local: 'false'
+    }
+  };
+  const parsedSetting  = Settings.parse({}, baseSettings, {}, 'browserstack.chrome_mac');
+  assert.strictEqual(parsedSetting.selenium.host, 'hub-cloud.browserstack.com');
+  assert.strictEqual(parsedSetting.selenium.port, 443);
+  assert.deepStrictEqual(parsedSetting.desiredCapabilities, expectedDesiredCapabilites);
+  assert.deepStrictEqual(parsedSetting.desiredCapabilities.chromeOptions, {w3c: false});
+});