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

cypress-api-mock

Package Overview
Dependencies
Maintainers
5
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cypress-api-mock

Cypress plugin for mocking API

  • 5.6.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
5
Created
Source

cypress-api-mock

Cypress plugin with commands for mocking API. It runs its own http server locally and returns registered responses. Http server can be started separately on remote machine if needed.

Adding to project

Add following lines to your commands.ts:

/// <reference types=cypress-api-mock" />

import "cypress-api-mock/commands";

Add following lines to your plugins/index.ts:

import apiMock from "cypress-api-mock/plugin";

function register(on: Cypress.PluginEvents, config: Cypress.ConfigOptions): void {
    /* your other registrations */
    apiMock(on, config);
}

export = register;

It will start server in the default address http://localhost:3000,

Usage

Prepare environment

It is highly recommended to reset the plugin before every suite to remove all previously registered mocks and logged requests.

beforeEach(() => cy.apiMockReset());

You can also reset the log of requests and responses during the test suite. Requests, responses and mocks are deleted once 5 minutes timeout after the mocks is written expires. If new mock is written, timeout is reset.

cy.apiMockResetCalls();

Mock specific API

cy.apiMock("/test-api", '{"id":${body.id},"message":"Hello ${body.name}!"}');

Assertion

cy.apiMockRequests().should((requests) => expect(requests[testApiUrl][0].data).to.eq(testApiRequestBody));
cy.apiMockResponses().should((requests) => expect(requests[testApiUrl][0]).to.eq(testApiExpectedResponse));

How to Develope

To build plugin run webpack with specific config:

For running server locally when cypress is started

npx webpack --config webpack.config.plugin.ts -w

For running server remotely

npx webpack --config webpack.config.server.ts -w

and then run cypress:

npx cypress open

FAQs

Package last updated on 20 Jul 2023

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