All Projects → PinkyJie → cypress-auto-stub-example

PinkyJie / cypress-auto-stub-example

Licence: MIT License
Example project to demonstrate how to record/replay API with Cypress.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to cypress-auto-stub-example

quasar-testing
Testing Harness App Extensions for the Quasar Framework 1.0+
Stars: ✭ 142 (+173.08%)
Mutual labels:  cypress, e2e-testing
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-23.08%)
Mutual labels:  cypress, e2e-testing
Cypress
Fast, easy and reliable testing for anything that runs in a browser.
Stars: ✭ 35,145 (+67486.54%)
Mutual labels:  cypress, e2e-testing
cypress-plugin-stripe-elements
A small Cypress plugin that assists you in filling in Stripe Elements inputs
Stars: ✭ 22 (-57.69%)
Mutual labels:  cypress, e2e-testing
odoo-cypress
Odoo Framework E2E Testing using Cypress
Stars: ✭ 19 (-63.46%)
Mutual labels:  cypress, e2e-testing
cypress-upload-file-post-form
Solution for two Cypress testing use-cases I came across with: perform a direct http FORM request to the server containing a file and other parameters and upload a file into a form before submission
Stars: ✭ 59 (+13.46%)
Mutual labels:  cypress, e2e-testing
xf-application-mocks
Contains lightweight substitutes for SAP applications to ease the development and testing of extension and integration scenarios. In conjunction with SAP Business Technology Platform, kyma runtime, the efficient implementation of application extensions is supported without the need for real SAP applications being accessible during development.
Stars: ✭ 21 (-59.62%)
Mutual labels:  mocking
mock-inspect
Mocks network requests and allows you to make assertions about how these requests happened. Supports auto-mocking of graphQL requests given a valid schema.
Stars: ✭ 19 (-63.46%)
Mutual labels:  mocking
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-7.69%)
Mutual labels:  mocking
metasfresh-webui-frontend-legacy
metasfresh Webui Frontend
Stars: ✭ 57 (+9.62%)
Mutual labels:  cypress
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (+48.08%)
Mutual labels:  mocking
vue3-realworld-example-app
Explore the charm of Vue composition API! Vite?
Stars: ✭ 364 (+600%)
Mutual labels:  cypress
cypress-xhr-responses-recording
No description or website provided.
Stars: ✭ 19 (-63.46%)
Mutual labels:  cypress
cypress-example-docker-compose
Example running Cypress tests against Apache server via docker-compose
Stars: ✭ 106 (+103.85%)
Mutual labels:  cypress
MockitoIn28Minutes
Learn Mockito from In28Minutes
Stars: ✭ 95 (+82.69%)
Mutual labels:  mocking
awesome-address-book
This project shows a basic address book built with ReactJS, Redux Toolkit and Typescript 📖
Stars: ✭ 20 (-61.54%)
Mutual labels:  cypress
elixir mock
Creates clean, concurrent, inspectable mocks from elixir modules
Stars: ✭ 21 (-59.62%)
Mutual labels:  mocking
dummyjdbc
dummyjdbc answers database requests with dummy data to be independent of an existing database.
Stars: ✭ 20 (-61.54%)
Mutual labels:  mocking
MockDataGenerator
Generate mock data for POCO
Stars: ✭ 12 (-76.92%)
Mutual labels:  mocking
next-ts-graphql-apollo-starter
An opiniated Next powered starter which include support for Apollo with GraphQL SSR support, codegen, styled component / system, framer motion and Cypress
Stars: ✭ 18 (-65.38%)
Mutual labels:  cypress

cypress-auto-stub-example CI cypress version cypress-auto-stub-example

This is an example project to demonstrate how to automatically stub all API requests happened in your Cypress test and how to record/replay them.

We are available on Cypress Dashboard, go there to check the running results. (Registration required)

Problems to solve

1. E2E tests take a long time to run, they are too slow.

One concern of running E2E tests is that it takes long time to run, which makes sense because all your API calls are targeted to the real server. To resolve this issue, a single solution is to use mock API in your E2E tests. That's good, but here comes another question: you need to manually update your mocking data according to the API change regularly.

A better solution is:

  • Use real API response when you write E2E tests, and Record these responses and write them into files.
  • Use the recorded API response to run your tests in future.

This project demonstrates how to make this process more smoothly and automatically with Cypress.

2. E2E tests are too flaky, how many seconds do I need to wait?

Though Cypress already ships with a unique mechanism to automatically block your test and retry until your expectation meets, but sometimes you still need to explicitly wait for all network API calls to be finished. In this example we implement a new command called waitUntilAllAPIFinished to solve this problem. Thanks to Cypress's full network control ability, now it's easier to know how many network API calls are still pending, so that we can wait for them to be finished first before you do any assertion.

Try it

  • Clone this project.
  • Install all dependencies by running yarn install or npm install.
  • Build the website by running yarn build or npm run build.
  • Launch the website by running yarn serve or npm run serve.
  • Open Cypress byr running yarn cy:open, then run the test network.spec.ts: (the same mechanism as Jest snapshot testing)
    • If a snapshot file does not exist inside the fixture folder, tests will be running with the real API, and after all tests are passed, a snapshot file containing all API responses will be generated in the fixture folder.
    • If a snapshot file does exist inside the fixture folder, but fixture data for this test case does not exist (e.g. there is no key with the same name as the test case name existing inside that snapshot file), tests will be running with the real API, then the snapshot file will be updated with the recorded API responses.
    • If a snapshot file in the fixture folder already exists and has the mocking data for this test case, the test should run very fast, because all API responses are stubbed and use the snapshot file as responses.
  • If you want to update the snapshot file regardless of the existing fixture file existence, use yarn cy:open:record. When using this command, all tests will run against the real API, and all returned API responses will override the existing snapshot file if it exists.
  • You can also use yarn cy:run to run all Cypress tests in headless mode.

More details for demo Edit cypress-auto-stub-example

The website page we are testing in this project contains 3 examples for XHR and Fetch:

  • Example 1:
  • Example 2:
    • Click a button to call API (POST https://reqres.in/api/users) using Fetch. API response JSON will be displayed below after it returns.
    • To demonstrate Fetch can be supported here.
  • Example 3:
    • Click a button to call 30 sequential API (GET https://reqres.in/api/users/1 to https://reqres.in/api/users/10 for 3 times) using Fetch. API response JSON (userId: 10) will be displayed below after it returns. sequential here mean the 2nd API call will file only after the 1st API call returns.
    • To demonstrate the usage of a new custom command cy.waitUntilAllAPIFinished() which will make sure the testing is blocked until all API calls are finished.

It also includes 2 examples for GraphQL:

Currently when you use cy.route() to mock API response with fixtures, Cypress only match the URL and HTTP method you provided, for most of the cases, this is enough for mocking purpose. However, if you use libraries like GraphQL, all API requests are using the same URL with different request body, for this case, you need to mock different responses based on different request bodies, which is not possible before Cypress fixes #687. Note: Cypress's cy.intercept() introduced after v6 already has full support to monitor request body, but the follow solution is still handy and faster. A simple workaround is: use md5 or other hash library to hash your request body into a string, and put this string as a query parameter in your URL, like ?_md5=xxxx, now your URL contains the request body information: different request bodies result in different md5 hash string, thus you can rely on URL and HTTP method to do API mocking with request body in consideration.

  • Example 1:
    • Click a button to query all users (Query https://api.spacex.land/) using GraphQL query. API response JSON will be displayed below after it returns.
  • Example 2:
    • Click a button to insert a new user (Mutation https://api.spacex.land/) using GraphQL mutation. API response JSON will be displayed below after it returns.

More detail for implementation

Inspired by this article and Jest snapshot testing.

  • Check the main implementation in cypress/support/utils/auto-stub.ts

    1. use cy.intercept to monitor all API requests
    2. use cy.writeFile to write all recorded API responses in a fixture file
    3. while replaying, use cy.fixture to load API responses from the fixture file
  • Check the implementation for custom command cy.waitUntilAllAPIFinished in cypress/support/commands.ts

    1. maintain an internal counter for API calls: increase 1 when filing a new API request, decrease 1 when receiving a API response
    2. use Cypress's Automatic Retry Assertion to regularly check this counter, block the testing until it equals to 0
  • Check the custom URI configuration(adding ?md5=xxx) for GraphQL in src/App.js

There are 4 new configuration parameters introduced in this example:

All configuration parameters are defined under env as environment variables because:

  • It's easier for overriding by cypress open --env foo=bar
  • All existing configurations provided by Cypress are strongly typed, you can't add new configuration type when you use Typescript.
Parameter Type Description Required Default Value
forceAPIRecording boolean force enabling auto record (used to update api mocking data) false
apiHosts string (multiple host separated by ,) URL for API endpoint
stubAPIPatterns string (multiple pattern separated by ,, will be used in new RegExp(xxx)) API pattern needs to be stubbed
apiMaxWaitingTime number (in milliseconds) used by cy.waitUntilAllAPIFinished, the maximum time when we wait for all API requests to be finished 60000 (60s)

Note: apiHosts and stubAPIPatterns must be aligned with each others.

The generated recording file inside cypress/fixtures folder has the following format.

{
  "Test case name": {
    "timestamp": "2018-08-26T07:27:00.582Z",
    "records": [
      "url": "xxx",
      "method": "xxx",
      "request": {
        "body": {},
      },
      "response": {
        "body": {},
      },
      // this means which host/pattern this API matches
      "matchHostIndex": 0,
    ]
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].