Repository: Codeception/CodeceptJS
Pull request #1951 (2019-10-13 20:59:35)

Config hooks tests


Patch:
diff --git a/examples/codecept.conf.js b/examples/codecept.conf.js
index 6443b875..c8c324d1 100644
--- a/examples/codecept.conf.js
+++ b/examples/codecept.conf.js
@@ -1,9 +1,3 @@
-const { useHeadlessWhen, useSharedCookies } = require('@codeceptjs/config-hooks');
-
-useHeadlessWhen(process.env.headless);
-useSharedCookies();
-useWindowSize(1600, 1000);
-
 exports.config = {
   output: './output',
   helpers: {
@@ -12,7 +6,7 @@ exports.config = {
       browser: 'chrome',
       restart: false,
       windowSize: '1600x1200',
-      show: false,
+      show: true,
     },
     REST: {},
   },
diff --git a/lib/config.js b/lib/config.js
index c333ed10..4ca0b712 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -1,8 +1,11 @@
 const fs = require('fs');
 const path = require('path');
-const isFile = require('./utils').isFile;
-const fileExists = require('./utils').fileExists;
-const deepMerge = require('./utils').deepMerge;
+const {
+  fileExists,
+  isFile,
+  deepMerge,
+  deepClone,
+} = require('./utils');
 
 const defaultConfig = {
   output: './_output',
@@ -33,7 +36,7 @@ class Config {
    * @param {*} newConfig
    */
   static create(newConfig) {
-    config = deepMerge(defaultConfig, newConfig);
+    config = deepMerge(deepClone(defaultConfig), newConfig);
     hooks.forEach(f => f(config));
     return config;
   }
@@ -104,7 +107,7 @@ class Config {
    */
   static reset() {
     hooks = [];
-    return config = defaultConfig;
+    return config = Object.assign({}, defaultConfig);
   }
 }
 
diff --git a/package.json b/package.json
index 838d3d36..72f9310a 100644
--- a/package.json
+++ b/package.json
@@ -39,6 +39,7 @@
     "dev:graphql": "nodemon test/data/graphql/index.js"
   },
   "dependencies": {
+    "@codeceptjs/configure": "^0.3.0",
     "allure-js-commons": "^1.3.2",
     "axios": "^0.19.0",
     "chalk": "^1.1.3",
@@ -73,10 +74,10 @@
     "@types/inquirer": "^0.0.35",
     "@types/mocha": "^5.2.7",
     "@types/node": "^8.10.54",
-    "@wdio/sauce-service": "^5.12.5",
-    "@wdio/selenium-standalone-service": "^5.12.1",
-    "@wdio/utils": "^5.13.0",
-    "apollo-server-express": "^2.9.3",
+    "@wdio/sauce-service": "^5.13.2",
+    "@wdio/selenium-standalone-service": "^5.13.2",
+    "@wdio/utils": "^5.13.2",
+    "apollo-server-express": "^2.9.4",
     "chai": "^3.4.1",
     "chai-as-promised": "^5.2.0",
     "documentation": "^8.1.2",
@@ -86,7 +87,7 @@
     "eslint-plugin-mocha": "^5.3.0",
     "express": "^4.17.1",
     "faker": "^4.1.0",
-    "graphql": "^14.5.7",
+    "graphql": "^14.5.8",
     "husky": "^1.2.1",
     "json-server": "^0.10.1",
     "nightmare": "^3.0.2",
@@ -99,7 +100,7 @@
     "testcafe": "^1.5.0",
     "typescript": "^2.9.2",
     "wdio-docker-service": "^1.5.0",
-    "webdriverio": "^5.13.1",
+    "webdriverio": "^5.13.2",
     "xmldom": "^0.1.27",
     "xpath": "0.0.27"
   },


Test patch:
diff --git a/lib/helper/TestCafe.js b/lib/helper/TestCafe.js
index e001ed6f..acf959d9 100644
--- a/lib/helper/TestCafe.js
+++ b/lib/helper/TestCafe.js
@@ -86,7 +86,6 @@ class TestCafe extends Helper {
   constructor(config) {
     super(config);
 
-    this.iteration = 1;
     this.testcafe = undefined; // testcafe instance
     this.t = undefined; // testcafe test controller
     this.dummyTestcafeFile; // generated testcafe test file
@@ -136,7 +135,7 @@ class TestCafe extends Helper {
 
     this.iteration += 2; // Use different ports for each test run
     // @ts-ignore
-    this.testcafe = await createTestCafe('localhost', 1338 + this.iteration, 1339 + this.iteration);
+    this.testcafe = await createTestCafe('localhost', null, null);
 
     this.debugSection('_before', 'Starting testcafe browser...');
 
diff --git a/test/unit/config_test.js b/test/unit/config_test.js
new file mode 100644
index 00000000..30ce1c15
--- /dev/null
+++ b/test/unit/config_test.js
@@ -0,0 +1,64 @@
+const config = require('../../lib/config');
+const { expect } = require('chai');
+
+describe('Config', () => {
+  beforeEach(() => config.reset());
+
+  it('should be created', () => {
+    const cfg = config.create({
+      output: './report',
+    });
+    expect(cfg).to.contain.keys(['helpers', 'plugins', 'include']);
+    expect(config.get()).to.eql(cfg);
+    expect(cfg.output).to.eql('./report');
+    expect(config.get('output')).to.eql('./report');
+    expect(config.get('output', './other')).to.eql('./report');
+    expect(config.get('tests', '**_test.js')).to.eql('**_test.js');
+  });
+
+
+  it('should be completely reset', () => {
+    config.addHook((cfg) => {
+      cfg.helpers.Puppeteer.show = true;
+    });
+    config.create({
+      tests: '**tests',
+      helpers: {
+        Puppeteer: {},
+      },
+    });
+    config.append({
+      output: './other',
+    });
+    expect(config.get('helpers').Puppeteer.show).to.eql(true);
+    config.reset();
+    expect(config.get().output).to.not.eql('./other');
+    expect(config.get()).to.not.contain.key('tests');
+    expect(config.get('helpers')).to.not.contain.key('Puppeteer');
+    config.create({
+      helpers: {
+        Puppeteer: {},
+      },
+    });
+    expect(config.get('helpers').Puppeteer.show).to.not.eql(true);
+  });
+
+  it('can be updated', () => {
+    config.create();
+    config.append({
+      output: './other',
+    });
+    expect(config.get('output')).to.eql('./other');
+  });
+
+  it('should use config hooks to enhance configs', () => {
+    config.addHook((cfg) => {
+      cfg.additionalValue = true;
+    });
+    const cfg = config.create({
+      additionalValue: false,
+    });
+    expect(cfg).to.contain.key('additionalValue');
+    expect(cfg.additionalValue).to.eql(true);
+  });
+});