Giter VIP home page Giter VIP logo

cypress-auto-mock-http-request's Introduction

cypress-auto-mock-http-request

semantic-release Commitizen friendly

Copy Notes

This repository is a copy of https://github.com/scottschafer/cypressautomocker. We have made this copy for an immediate need to use the library and the correction of specific issues found. We suggest using the original library because these same changes will be suggested to the original library

How to use

Integrating this tool into your web application involves a few steps:

  1. Add the cypress-auto-mock-http-request to your project:

    npm install --save cypress-auto-mock-http-request
  2. Add the cypress web hooks to your application.

    import installCypressHooks from 'cypress-auto-mock-http-request/include-in-webapp';
    installCypressHooks();

    Another option to do the same thing would be to include the following code in your HTML instead:

    <script src="node_modules/cypress-auto-mock-http-request/include-in-webapp/installCypressHooks-norequire.js">
  3. Add the following to cypress/support/commands.js

    import registerAutoMockCommands from 'cypress-auto-mock-http-request/include-in-tests';
    registerAutoMockCommands();
  4. In each of your tests, add the following:

      const MOCK_FILENAME = 'testCounter';
    
      before(() => {
        cy.automock(MOCK_FILENAME);
      });
    
      after(() => {
        cy.automockEnd();
      });

This tool is built on top of the open-source testing platform Cypress.io to allow recording API results and replaying the APIs as a mock server.

cypress auto mocker example tests running

Folder layout

There are three subfolders within the head directory:

  1. example: Contains a simple application to test against that implements APIs with changing responses, along with an example Cypress test.
  2. include-in-tests: Contains a library to include in your in your test suite,
  3. include-in-webapp: Contains a library to include in your application that is being tested.
Running the example

To install and run the example, run the following:

cd example
npm install
npm run start

You should see Example app listening at http://localhost:1337 which means that the local server is running on port 1337. It will also open Cypress, allowing you to run the test.

The first time you run the test, our tool will record the API results to the file example/cypress/automocks/testCounter.json. The next time the test is run, it will automatically use the contents of the file to mock the APIs.

You can control the recording and playback behavior using the (optional) automocker field in cypress.json:

  "automocker": {
    "record": true,
    "playback": true
  }

The default (that is, if "automocker" doesn't exist) is to treat both record and playback as true, which means that it will automatically record API calls (if the proper commands are called in the tests) if the mock file does not exits, and will play them back as mocks if they do exist.

cypress-auto-mock-http-request's People

Contributors

aperdomob avatar dependabot[bot] avatar jpinerosg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.