Repository: Codeception/CodeceptJS
Pull request #1638 (2019-05-05 20:43:21)

Fixed loading page objects via inject

codacy-bot: 
![Codacy](https://app.codacy.com/assets/images/favicon.png) Here is an overview of what got changed by this pull request:


```diff

Clones added
============
- test/unit/container_test.js  2
         
```


See the complete overview on [Codacy](https://app.codacy.com/app/DavertMik/CodeceptJS/pullRequest?prid=3538802&bid=12590407)
        

Patch:
diff --git a/docs/changelog.md b/docs/changelog.md
index c696ecfa..b4956c7a 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -1,3 +1,7 @@
+## 2.1.1
+
+* Do not retry `within` and `session` calls inside `retryFailedStep` plugin. Fix by @tsuemura
+
 ## 2.1.0
 
 * Added global `inject()` function to require actor and page objects using dependency injection. Recommended to use in page objects, step definition files, support objects:
diff --git a/docs/examples.md b/docs/examples.md
index 90a52fb2..1f2c3a9c 100644
--- a/docs/examples.md
+++ b/docs/examples.md
@@ -88,3 +88,8 @@ This repository demonstrates usage of
 * Working with Angular CLI
 * Reports with Mochawesome helper
 
+## [REST Example Tests](https://github.com/PeterNgTr/codeceptjs-rest-demo)
+
+This repository demonstrates usage of
+
+* REST helper
diff --git a/lib/container.js b/lib/container.js
index 0bce63e1..e8227b2b 100644
--- a/lib/container.js
+++ b/lib/container.js
@@ -32,8 +32,8 @@ class Container {
     container.mocha = MochaFactory.create(mochaConfig, opts || {});
     container.helpers = createHelpers(config.helpers || {});
     container.translation = loadTranslation(config.translation || null);
-    container.support = createSupportObjects(config.include || {});
     container.plugins = createPlugins(config.plugins || {}, opts);
+    container.support = createSupportObjects(config.include || {});
     if (config.gherkin) loadGherkinSteps(config.gherkin.steps || []);
   }
 
@@ -162,18 +162,11 @@ function createHelpers(config) {
 
 function createSupportObjects(config) {
   const objects = {};
+
   for (const name in config) {
-    objects[name] = getSupportObject(config, name);
-    try {
-      if (typeof objects[name] === 'function') {
-        objects[name] = objects[name]();
-      } else if (objects[name]._init) {
-        objects[name]._init();
-      }
-    } catch (err) {
-      throw new Error(`Initialization failed for ${objects[name]}\n${err.message}`);
-    }
+    objects[name] = {}; // placeholders
   }
+
   if (!objects.I) {
     objects.I = require('./actor')();
 
@@ -182,6 +175,21 @@ function createSupportObjects(config) {
     }
   }
 
+  container.support = objects;
+
+  for (const name in config) {
+    let newObj = getSupportObject(config, name);
+    try {
+      if (typeof newObj === 'function') {
+        newObj = newObj();
+      } else if (newObj._init) {
+        newObj._init();
+      }
+    } catch (err) {
+      throw new Error(`Initialization failed for ${name}: ${newObj}\n${err.message}`);
+    }
+    Object.assign(objects[name], newObj);
+  }
   const asyncWrapper = function (f) {
     return function () {
       return f.apply(this, arguments).catch((e) => {


Test patch:
diff --git a/test/data/I.js b/test/data/I.js
index 3928ac50..76a70cad 100644
--- a/test/data/I.js
+++ b/test/data/I.js
@@ -1,9 +1,7 @@
 module.exports = {
-
   _init: () => {
     global.I_initialized = true;
   },
 
   doSomething: () => 'done',
-
 };
diff --git a/test/data/dummy_page.js b/test/data/dummy_page.js
index 6a31d7a1..49c79338 100644
--- a/test/data/dummy_page.js
+++ b/test/data/dummy_page.js
@@ -1,3 +1,6 @@
+const { I } = inject();
+
 module.exports = {
   openDummyPage: () => 'dummy page opened',
+  getI: () => I,
 };
diff --git a/test/unit/container_test.js b/test/unit/container_test.js
index ed2a3013..7d3b2189 100644
--- a/test/unit/container_test.js
+++ b/test/unit/container_test.js
@@ -2,14 +2,21 @@ const container = require('../../lib/container');
 const assert = require('assert');
 const path = require('path');
 const FileSystem = require('../../lib/helper/FileSystem');
+const actor = require('../../lib/actor');
 
 describe('Container', () => {
   before(() => {
     global.codecept_dir = path.join(__dirname, '/..');
+    global.inject = container.support;
+    global.actor = actor;
   });
 
   afterEach(() => {
     container.clear();
+    ['I', 'dummy_page'].forEach((po) => {
+      const name = require.resolve(path.join(__dirname, `../data/${po}`));
+      delete require.cache[name];
+    });
   });
 
 
@@ -121,7 +128,7 @@ describe('Container', () => {
         },
       });
       assert.ok(container.support('I'));
-      container.support('I').should.have.keys('_init', 'doSomething');
+      container.support('I').should.include.keys('_init', 'doSomething');
       assert(global.I_initialized);
     });
 
@@ -132,6 +139,44 @@ describe('Container', () => {
         },
       });
       assert.ok(container.support('dummyPage'));
+      container.support('dummyPage').should.include.keys('openDummyPage');
+    });
+
+    it('should load DI and inject I into PO', () => {
+      container.create({
+        include: {
+          dummyPage: './data/dummy_page',
+        },
+      });
+      assert.ok(container.support('dummyPage'));
+      assert.ok(container.support('I'));
+      container.support('dummyPage').should.include.keys('openDummyPage');
+      container.support('dummyPage').getI().should.have.keys(Object.keys(container.support('I')));
+    });
+
+
+    it('should load DI and inject custom I into PO', () => {
+      container.create({
+        include: {
+          dummyPage: './data/dummy_page',
+          I: './data/I',
+        },
+      });
+      assert.ok(container.support('dummyPage'));
+      assert.ok(container.support('I'));
+      container.support('dummyPage').should.include.keys('openDummyPage');
+      container.support('dummyPage').getI().should.include.keys(Object.keys(container.support('I')));
+    });
+
+    it('should load DI includes provided as objects', () => {
+      container.create({
+        include: {
+          dummyPage: {
+            openDummyPage: () => 'dummy page opened',
+          },
+        },
+      });
+      assert.ok(container.support('dummyPage'));
       container.support('dummyPage').should.have.keys('openDummyPage');
     });