@talend/cypress-api-mock-plugin

1.2.2 • Public • Published

@talend/cypress-api-mock-plugin

This Cypress plugin allows to mock any http request via HAR files.

It has 2 modes:

  • record http requests into HAR (HTTP Archive) files
  • mock http requests from HAR files

Installation

npm install --save-dev @talend/cypress-api-mock-plugin

// or

yarn add -D @talend/cypress-api-mock-plugin

Edit your cypress/plugins/index.js

const { installApiMock, ensureBrowserFlags } = require('@talend/cypress-api-mock-plugin');

module.exports = async (on, config) => {
    installApiMock(on, config);

	on('before:browser:launch', (browser = {}, launchOptions) => {
		ensureBrowserFlags(browser, launchOptions);
		return launchOptions;
	});
}

Edit your cypress/support/commands.js

import { installApiMockCommands } from '@talend/cypress-api-mock-plugin/commands';

installApiMockCommands();

Usage

In each spec files you want to record/mock the hhtp requests, add a before/after call.

describe('my spec', () => {
    before(() => {
		cy.beforeApiMock();
	});

	after(() => {
		cy.afterApiMock();
	});
});

Run

To record the requests, you can pass a record-api environment variable.
Under the hood, the plugin uses @neuralegion/cypress-har-generator that needs to run on chrome.

cypress run --browser chrome --headless --env record-api=true

Once you have the api mock har files, to run the tests with the api mock:

cypress run

Readme

Keywords

Package Sidebar

Install

npm i @talend/cypress-api-mock-plugin

Weekly Downloads

11

Version

1.2.2

License

Apache-2.0

Unpacked Size

8.94 kB

Total Files

7

Last publish

Collaborators

  • talend-frontend