Protractor plugin used to mock backend calls.
-
Add NPM dependency to your
package.json
:npm install --save-dev protractor-backend-mock-plugin
-
Add & Configure Protractor plugin:
// protractor.conf.js exports.config = { plugins: [ { package: 'protractor-backend-mock-plugin', // config backend: [8080, 'localhost'], fake: [9999, 'localhost'] } ] }
-
Use Nock to mock results from
fake
server:npm install --save-dev nock
import * as nock from 'nock'; beforeEach(() => { nock('http://localhost:9999').get(`/user/me`).reply(200, {id: 42, name: 'John Doe'}) });
An Express.js server is started locally, to intercept webapp calls.
In order to intercept and mock calls (using nock
), request must be emitted inside NodeJS application.
So all request are redirected to another URL, defined by fake
section.
+-------------------------------------------+
| NodeJS |
+--------+ | +---------+ +------+ +------+ |
| Webapp + --> | Backend + --> proxy ->| Nock | | Fake | |
+--------+ | +---------+ +------+ +------+ |
+-------------------------------------------+