Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-mock-record

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-mock-record

An angular / protractor framework that mocks and records requests. Requests can be manually mocked or recorded like VCR.

  • 1.6.4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-75%
Maintainers
2
Weekly downloads
 
Created
Source

angular-mock-record

An Angular 4+ framework that mocks and records requests. Requests can be manually mocked or recorded like VCR.

Installation

  • Run npm install angular-mock-record
  • Create a mock.record.config.json file in the root of your Angular app:
{
  "domain": "http://your.api.domain",
  "port": 3000,
  "cors": false,
  "tape_name": "vcr",
  "proxied_mock_server_route": "/e2e",
  "request_headers": [
    "X-XSRF-TOKEN",
    "cookie"
  ],
  "recording_dir": "./e2e/mocks",
  "allow_recording": false,
  "exclude_params": [],
  "normalize_params": []
}

Mocking

  • Import in the protractor spec file:

import { MockUtilities } from 'angular-mock-record/client/mock.utilities';

  • Then, mock requests in beforeEach or beforeAll:

beforeAll(() => { mockUtilities.mockRequest('path/yourRequest.json', {foo: 'bar'}); )};

  • Next, run tests in the describe block:

it('should have the mocked request data', () => { expect(page.getFoo().getText()).toContain('bar'); });

  • Finally, be sure to clearMocks before moving to a new describe:

afterAll(() => { mockUtilities.clearMocks(); });

Setting up mock.record.config.json for recording

  • Set domain as the request path that will be used to make requests and record.
  • Set an array of params that need to be excluded from request url matching via exclude_params. Ex: exclude_params: ['sort']
  • Set an array of params that need to be normalized in request url via normalize_params. Ex: normalize_params: ['randomly_generated_id']
  • Set allow_recording to true to fail when a new recording is detected. This is handy for continuous integration tools such as Travis CI.

Functionality

  • To set the client to login as (If not specified, product demo a is the default): mockUtilities.setClient( clientOverride.getClientDomain(<client>) );

  • To login and record authenticated requests, use: mockUtilities.login(<user from idp auth>);

  • To set a "context" and limit new recordings to the scope of that context: mockUtilities.setContext(<name of context>);

  • As usual, clear the mocks in the afterAll block at the end of the spec file. This will also reset context, client and login status: mockUtilities.clearMocks();

Running the server

  • Run node ./node_modules/angular-mock-record/server/server.js allow_recording
  • Hit http://localhost:<port>/<api_path> in a browser to test the recording functionality. Once recorded, requests matching this URL will return the captured recording.
  • Finally, set up your application's E2E endpoint configuration to point to http://localhost:<port>/<api_path>. Start the server, then run ng e2e. All requests will be recorded unless they are otherwise mocked.

Publishing to NPM

  • bump the version number in package.json
  • run npm publish locally

Keywords

FAQs

Package last updated on 10 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc