@mocks-server/cypress-commands

    4.0.0 • Public • Published

    Mocks Server logo

    Build Status Coverage Quality Gate Downloads Renovate 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")

    Set current mock.

    cy.mocksUseRouteVariant("users:success")

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

    cy.mocksRestoreRoutesVariants()

    Restore routes variants to those defined in current mock.

    cy.mocksSetDelay(2000)

    Set routes delay.

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

    Set any Mocks Server setting.

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

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

    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");
      });
    });

    License

    MIT, see LICENSE for details.

    Install

    npm i @mocks-server/cypress-commands

    DownloadsWeekly Downloads

    855

    Version

    4.0.0

    License

    MIT

    Unpacked Size

    13.5 kB

    Total Files

    7

    Last publish

    Collaborators

    • javierbrea