Repository: Codeception/CodeceptJS
Pull request #1758 (2019-07-07 02:01:25)

Updated docs for Polly

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/acceptance/codecept.Puppeteer.js  1
         
```


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

Patch:
diff --git a/docs/helpers/Polly.md b/docs/helpers/Polly.md
new file mode 100644
index 00000000..3cb32a9e
--- /dev/null
+++ b/docs/helpers/Polly.md
@@ -0,0 +1,75 @@
+---
+id: Polly
+title: Polly
+---
+
+<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
+
+## Polly
+
+Extends Helper
+
+This helper allows to mock requests while running tests in Puppeteer.
+For instance, you can block calls to 3rd-party services like Google Analytics, CDNs.
+Another way of using is to emulate requests from server by passing prepared data.
+
+### Installations
+
+Requires [Polly.js][1] library by Netflix installed
+
+    npm i @pollyjs/core @pollyjs/adapter-puppeteer --save-dev
+
+Requires Puppeteer helper enabled
+
+### Configuration
+
+Just enable helper in config file:
+
+```js
+helpers: {
+   Puppeteer: {
+     // regular Puppeteer config here
+   },
+   Polly: {}
+}
+```
+
+### Usage
+
+Use `I.mockRequest` to intercept and mock requests.
+
+### Parameters
+
+-   `config`  
+
+### \_checkAndStartMocking
+
+Starts mocking if it's not started yet.
+
+### mockRequest
+
+Mock response status
+
+```js
+I.mockRequest('GET', '/api/users', 200);
+I.mockRequest('ANY', '/secretsRoutes/', 403);
+I.mockRequest('POST', '/secrets', { secrets: 'fakeSecrets' });
+```
+
+Multiple requests
+
+```js
+I.mockRequest('GET', ['/secrets', '/v2/secrets'], 403);
+```
+
+#### Parameters
+
+-   `method`  
+-   `oneOrMoreUrls`  
+-   `dataOrStatusCode`  
+
+### stopMocking
+
+Stops mocking requests.
+
+[1]: https://netflix.github.io/pollyjs/#/
diff --git a/lib/container.js b/lib/container.js
index c6f5ee94..a0f85cb6 100644
--- a/lib/container.js
+++ b/lib/container.js
@@ -161,23 +161,23 @@ function createHelpers(config) {
 }
 
 function createSupportObjects(config) {
-  const objects = {};
-
-  for (const name in config) {
-    objects[name] = {}; // placeholders
-  }
+  const objects = container.support = new Proxy({}, {
+    get(target, key) {
+      // configured but not in support object, yet: load the module
+      if (key in config && !(key in target)) target[key] = lazyLoad(key);
+      return target[key];
+    },
+  });
 
-  if (!objects.I) {
-    objects.I = require('./actor')();
+  if (!config.I) {
+    container.support.I = require('./actor')();
 
     if (container.translation.I !== 'I') {
-      objects[container.translation.I] = objects.I;
+      container.support[container.translation.I] = container.support.I;
     }
   }
 
-  container.support = objects;
-
-  for (const name in config) {
+  function lazyLoad(name) {
     let newObj = getSupportObject(config, name);
     try {
       if (typeof newObj === 'function') {
@@ -188,7 +188,7 @@ function createSupportObjects(config) {
     } catch (err) {
       throw new Error(`Initialization failed for ${name}: ${newObj}\n${err.message}`);
     }
-    Object.assign(objects[name], newObj);
+    return newObj;
   }
   const asyncWrapper = function (f) {
     return function () {
diff --git a/lib/helper/Polly.js b/lib/helper/Polly.js
index c66e67e2..410ef1e2 100644
--- a/lib/helper/Polly.js
+++ b/lib/helper/Polly.js
@@ -4,6 +4,39 @@ const { appendBaseUrl } = require('../utils');
 
 let PollyJS;
 
+/**
+ * This helper allows to **mock requests while running tests in Puppeteer**.
+ * For instance, you can block calls to 3rd-party services like Google Analytics, CDNs.
+ * Another way of using is to emulate requests from server by passing prepared data.
+ *
+ * ### Installations
+ *
+ * Requires [Polly.js](https://netflix.github.io/pollyjs/#/) library by Netflix installed
+ *
+ * ```
+ * npm i @pollyjs/core @pollyjs/adapter-puppeteer --save-dev
+ * ```
+ *
+ * Requires Puppeteer helper enabled
+ *
+ * ### Configuration
+ *
+ * Just enable helper in config file:
+ *
+ * ```js
+ * helpers: {
+ *    Puppeteer: {
+ *      // regular Puppeteer config here
+ *    },
+ *    Polly: {}
+ * }
+ * ```
+ *
+ * ### Usage
+ *
+ * Use `I.mockRequest` to intercept and mock requests.
+ *
+ */
 class Polly extends Helper {
   constructor(config) {
     super(config);
@@ -19,9 +52,7 @@ class Polly extends Helper {
     }
   }
 
-  /**
-   * Start mocking network requests/responses
-   */
+  // Start mocking network requests/responses
   async _startMocking(title = 'Test') {
     if (!this.helpers && !this.helpers.Puppeteer) {
       throw new Error('Puppeteer is the only supported helper right now');
@@ -29,9 +60,7 @@ class Polly extends Helper {
     await this._connectPuppeteer(title);
   }
 
-  /**
-   * Connect Puppeteer helper to mock future requests.
-   */
+  // Connect Puppeteer helper to mock future requests.
   async _connectPuppeteer(title) {
     const adapter = require('@pollyjs/adapter-puppeteer');
 
@@ -94,12 +123,10 @@ class Polly extends Helper {
     }
   }
 
-  /**
-   * Get route-handler of Polly for different HTTP methods
-   * @param {string} method HTTP request methods(e.g., 'GET', 'POST')
-   * @param {string|array} oneOrMoreUrls URL or array of URLs
-   * @param {string} baseUrl hostURL
-   */
+  // Get route-handler of Polly for different HTTP methods
+  // @param {string} method HTTP request methods(e.g., 'GET', 'POST')
+  // @param {string|array} oneOrMoreUrls URL or array of URLs
+  // @param {string} baseUrl hostURL
   _getRouteHandler(method, oneOrMoreUrls, baseUrl) {
     const { server } = this.polly;
 
@@ -113,7 +140,7 @@ class Polly extends Helper {
   }
 
   /**
-   * Stop mocking requests.
+   * Stops mocking requests.
    */
   async stopMocking() {
     if (!this._checkIfMockingStarted()) return;
diff --git a/lib/utils.js b/lib/utils.js
index fb75a897..d56ab4e9 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -333,6 +333,7 @@ module.exports.appendBaseUrl = function (baseUrl, oneOrMoreUrls) {
     return joinUrl(baseUrl, oneOrMoreUrls);
   }
   return oneOrMoreUrls.map(url => joinUrl(baseUrl, url));
+};
 
 /**
  * Recursively search key in object and replace it's value.


Test patch:
diff --git a/test/unit/container_test.js b/test/unit/container_test.js
index 7d3b2189..ea4369a9 100644
--- a/test/unit/container_test.js
+++ b/test/unit/container_test.js
@@ -121,6 +121,16 @@ describe('Container', () => {
       assert.ok(container.support('I'));
     });
 
+    it('should load DI and return a reference to the module', () => {
+      container.create({
+        include: {
+          dummyPage: './data/dummy_page',
+        },
+      });
+      const dummyPage = require('../data/dummy_page');
+      container.support('dummyPage').should.be.equal(dummyPage);
+    });
+
     it('should load I from path and execute _init', () => {
       container.create({
         include: {