@mocks-server/cypress-commands

Extends Cypress' cy commands with methods for administrating Mocks Server

Usage no npm install needed!

<script type="module">
  import mocksServerCypressCommands from 'https://cdn.skypack.dev/@mocks-server/cypress-commands';
</script>

README

Build status Coverage Status Quality Gate

Renovate Last commit Last release

NPM downloads License

Mocks Server Cypress commands

Extends Cypress' cy commands with methods for easily changing Mocks Server settings, such as current mock, route variants, delay time, etc.

Installation

This module is distributed via npm and should be installed as one of your project's devDependencies:

npm i --save-dev @mocks-server/cypress-commands

Usage

@mocks-server/cypress-commands extends Cypress' cy command.

Add this line to your project's cypress/support/commands.js:

import "@mocks-server/cypress-commands"

You can now use all next commands:

Commands

cy.mocksSetMock("users-error")

Sets current mock.

cy.mocksUseRouteVariant("users:success")

Sets a specific route variant to be used by current mock.

cy.mocksRestoreRoutesVariants()

Restore routes variants to those defined in current mock.

cy.mocksSetDelay(2000)

Sets delay time.

cy.mocksSetSettings({ watch: false, delay: 0 })

Sets any Mocks Server setting.

cy.mocksConfig({ adminApiPath: "/foo", baseUrl: "http://localhost:3000" })

Configures the Mocks Server administration API client, used under the hood.

cy.mocksSetBehavior("foo")

Legacy method that sets behavior in Mocks Server v1.x

Configuration

By default, the API client is configured to request to http://127.0.0.1:3100/admin, based in the default Mocks Server options

You can change both the base url of Mocks Server, and the api path of the administration API using the cy.mocksConfig command mentioned above, or the plugin environment variables:

  • MOCKS_SERVER_BASE_URL: Modifies the base url of Mocks Server. Default is http://127.0.0.1:3100.
  • MOCKS_SERVER_ADMIN_API_PATH: Modifies the path of the Mocks Server administration API. Default is /admin.
  • MOCKS_SERVER_ENABLED: Disables requests to Mocks Server, so the commands will not fail even when Mocks Server is not running. This is useful to reuse same tests with mocks and a real API, because commands to change Mocks Server settings will be ignored.

Using commands

You should usually change Mocks Server settings in a before statement:

describe("user with default role", () => {
  before(() => {
    cy.mocksSetMock("normal-user");
    cy.visit("/");
  });

  it("should not see the users section link", () => {
    cy.get("#users-section-link").should("not.be.visible");
  });
});

describe("user with admin role", () => {
  before(() => {
    cy.mocksSetMock("admin-user");
    cy.visit("/");
  });

  it("should see the users section link", () => {
    cy.get("#users-section-link").should("be.visible");
  });
});

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.

License

MIT, see LICENSE for details.