An angular / protractor framework that mocks and records requests. Requests can be manually mocked or recorded like VCR.
npm install angular-mock-recordnpm install angular-mock-recordmock.record.config.json file in the root of your Angular app:
``json`
{
"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": []
}
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.exclude_params
- Set an array of params that need to be excluded from request url matching via . Ex: exclude_params: ['sort']normalize_params
- Set an array of params that need to be normalized in request url via . Ex: normalize_params: ['randomly_generated_id']allow_recording
- Set to true to fail when a new recording is detected. This is handy for continuous integration tools such as Travis CI.
- To set the client to login as (If not specified, product demo a is the default):
mockUtilities.setClient( clientOverride.getClientDomain(
- To login and record authenticated requests, use:
mockUtilities.login(
- To set a "context" and limit new recordings to the scope of that context:
mockUtilities.setContext(
- 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();
- Run node ./node_modules/angular-mock-record/server/server.js allow_recordinghttp://localhost:
- Hit in a browser to test the recording functionality. Once recorded, requests matching this URL will return the captured recording.http://localhost:
- Finally, set up your application's E2E endpoint configuration to point to . Start the server, then run ng e2e. All requests will be recorded unless they are otherwise mocked.
- bump the version number in package.json
- run npm publish` locally