All Projects → scottschafer → Cypressautomocker

scottschafer / Cypressautomocker

cypress-based library for capturing APIs and replaying them as mocks

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cypressautomocker

Ms Identity Aspnet Daemon Webapp
A web application that sync's data from the Microsoft Graph using the identity of the application, instead of on behalf of a user.
Stars: ✭ 86 (-15.69%)
Mutual labels:  webapp
Tutorialdb
A search 🔎 engine for programming/dev tutorials, See it in action 👉
Stars: ✭ 93 (-8.82%)
Mutual labels:  webapp
Notepad
📒 An offline capable Notepad PWA powered by ServiceWorker
Stars: ✭ 100 (-1.96%)
Mutual labels:  webapp
Play Java Websocket Example
Example Play Java application showing Websocket usage with Akka actors
Stars: ✭ 86 (-15.69%)
Mutual labels:  webapp
Courtney
Courtney is a coverage tool for Go
Stars: ✭ 92 (-9.8%)
Mutual labels:  testing-tools
Gunit
xUnit-style test fixture adapter for go test
Stars: ✭ 94 (-7.84%)
Mutual labels:  testing-tools
Simple Reactjs App
Simple Application Using Basic React Components and Communication between them
Stars: ✭ 86 (-15.69%)
Mutual labels:  webapp
Mongodb Memory Server
Spinning up mongod in memory for fast tests. If you run tests in parallel this lib helps to spin up dedicated mongodb servers for every test file in MacOS, *nix, Windows or CI environments (in most cases with zero-config).
Stars: ✭ 1,376 (+1249.02%)
Mutual labels:  testing-tools
Navigation Timing
Navigation Timing
Stars: ✭ 92 (-9.8%)
Mutual labels:  webapp
Mailwatch
MailWatch for MailScanner is a web-based front-end to MailScanner
Stars: ✭ 99 (-2.94%)
Mutual labels:  webapp
Mockwebserverplus
✔️ OkHttp mockwebserver with fixtures extension
Stars: ✭ 87 (-14.71%)
Mutual labels:  testing-tools
Iver
ui component for vue2.0
Stars: ✭ 92 (-9.8%)
Mutual labels:  webapp
Vue3 Ts Template H5
🍰🤡 Vue3.0 + Typescript + Sass + Vant 移动端 ,附属微信朋友圈demo
Stars: ✭ 94 (-7.84%)
Mutual labels:  webapp
Flask Todolist
exemplary flask application - small to-do list WebApp example
Stars: ✭ 85 (-16.67%)
Mutual labels:  webapp
Uce Handler
Convenient Uncaught-Exception Handler Library For Testers and Developers. Copy, Share, Email, Save crash logs easily.
Stars: ✭ 101 (-0.98%)
Mutual labels:  testing-tools
Ongaku
Anime Radio
Stars: ✭ 86 (-15.69%)
Mutual labels:  webapp
Kotlinfixture
Fixtures for Kotlin providing generated values for unit testing
Stars: ✭ 94 (-7.84%)
Mutual labels:  testing-tools
Minimal Notes
Minimal Notes web app build with Vue.js
Stars: ✭ 102 (+0%)
Mutual labels:  webapp
Distort Grid
distort and warp images in the browser
Stars: ✭ 101 (-0.98%)
Mutual labels:  webapp
Absolute
Absolute is a web-based O2O(Online to Offline) platform like Siren-Order for Starbucks. We are focused on creating the platform that is portable, cheaper, easier, decentralized and appless.
Stars: ✭ 96 (-5.88%)
Mutual labels:  webapp

cypressautomocker

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.

Integrating Into Your Own Web Application and tests

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

  1. Add the cypressautomocker to your project:
npm install --save cypressautomocker
  1. Add the cypress web hooks to your application.
import installCypressHooks from 'cypressautomocker/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/cypressautomocker/include-in-webapp/installCypressHooks-norequire.js">
  1. Add the following to cypress/support/commands.js
import registerAutoMockCommands from 'cypressautomocker/include-in-tests';
registerAutoMockCommands();
  1. In each of your tests, add the following:
const MOCK_FILENAME = 'testCounter';

before(() => {
  cy.automock(MOCK_FILENAME);
});

after(() => {
  cy.automockEnd();
});

The cy.automock() takes an optional parameter which may contain a function named resolveMockFunc. This can be used to resolve to a different recorded mock than this library would normally pick. You can pass it like so:

cy.automock(MOCK_FILENAME, {
  resolveMockFunc: (request, mockArray, mock) => {
    console.log(request.method + ' ' + request.url);
    // just return the resolved mock. This is a no-op. We could resolve to a different one in mockArray
    return mock;
  }
});

If this function is passed, it will be called to look up a recorded mock. The function will be called with these parameters:

- request: XMLHttpRequest
- mockArray: Array<{
  method: string,
  path: string,
  query: string,
  request: string,
  response: string;
  status: number;
  statusText: string;
  contentType: string; }>;
- mock: an entry in mockArray that would be returned, or null
It should return either mock, or an entry from mockArray
  1. Wait for APIs to complete using cy.automockWaitOnPendingAPIs();

This library does not use fixtures, so there is no way to alias a route and wait for it to complete. However, the cy.automockWaitOnPendingAPIs() can be used to wait for outstanding APIs to complete before proceeding.

Issues and remedies

This library records API calls and replays them in the exact order in which they were recorded. It ignores all query parameters and uses a combination of the method and path as the key. This means that if GET /counter returns three different responses when the APIs are recorded, those same three responses will be replayed when the mock file is present.

If you change your tests so that APIs are called in a different order and you have a recorded mock file, your tests may fail. One solution is to simply delete the contents of the automocks folder and also the recording to be re-generated.

The mocked APIs will be much faster than actual calls to the server. In some cases, this could cause your tests to run differently. This can surface actual bugs in your web application, for example, your code might rely on one API to complete after another has completed, and faster APIs could surface race conditions.

If your tests cannot have deterministic outcomes, you may wish to use resolveMockFunc() as documented above.

This library has not been tested with code that reloads pages. It's possible that it could be made to work, although installCypressHooks would need to be called in both places. This usage is not recommended.

Project status

This project is not really being actively maintained. I have dipped into it occasionally, but we are using a different approach to testing now and so keeping this updated is not a top priority for me.

I hope you find it useful, and will post occasional updates if the need arises and time allows.

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].