Package Exports
- @websulik/cypress-autorecord-sulik
- @websulik/cypress-autorecord-sulik/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@websulik/cypress-autorecord-sulik) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Cypress Autorecord
Note: this is fork of https://github.com/Nanciee/cypress-autorecord
Due to some pull-request not getting merged:
https://github.com/Nanciee/cypress-autorecord/pull/30
https://github.com/Nanciee/cypress-autorecord/pull/29
Cypress Autorecord is a plugin built to be used with Cypress.io. It simplifies mocking by auto-recording/stubbing HTTP interactions and automating the process of updating/deleting recordings. Spend more time writing integration tests instead of managing your mock data.
Getting Started
Install from npm
npm install --save-dev cypress-autorecord
Add this snippet in your project's /cypress/plugins/index.js
const fs = require('fs');
const autoRecord = require('cypress-autorecord/plugin');
module.exports = (on, config) => {
autoRecord(on, config, fs);
};
To allow for auto-recording and stubbing to work, require cypress-autorecord in each of your test file and call the function at the beginning of your parent describe
block.
const autoRecord = require('cypress-autorecord'); // Require the autorecord function
describe('Home Page', function () {
// Do not use arrow functions
autoRecord(); // Call the autoRecord function at the beginning of your describe block
// Your hooks (beforeEach, afterEach, etc) goes here
it('...', function () {
// Do not use arrow functions
// Your test goes here
});
});
NOTE: Do not use ES6 arrow functions for your describe or it callback. This will cause the recording function to break.
That is it! Now, just run your tests and the auto-record will take care of the rest!
Updating Mocks
In the case you need to update your mocks for a particular test:
const autoRecord = require('cypress-autorecord');
describe('Home Page', function () {
autoRecord();
it('[r] my awesome test', function () {
// Insert [r] at the beginning of your test name
// ...
});
});
This will force the test to record over your existent mocks for ONLY this test on your next run.
This can also be done through the configurations by adding the test name in the file cypress.json
:
{
"autorecord": {
"recordTests": ["my awesome test"]
}
}
Alternatively, you can update recordings for all tests by setting forceRecord
to true
before rerunning your tests:
{
"autorecord": {
"forceRecord": true
}
}
Removing Stale Mocks
Stale mocks that are no longer being used can be automatically removed when you run your tests by setting cleanMocks
to true
in the file cypress.json
:
{
"autorecord": {
"cleanMocks": true
}
}
NOTE: Only mocks that are used during the run are considered "active". Make sure to only set cleanMocks
to true
when you are running ALL your tests. Remove any unintentional .only
or .skip
.
Set Recording Pattern For Cypress Intercept
By default autorecorder is recording all outgoing requests but if you want to record only specific calls based on pattern(Ex. just record api calls on backend), you can set interceptPattern
in cypress.json
. it can be string, regex or glob
{
"autorecord": {
"interceptPattern": "**/api/**"
}
}
Ignoring Request Body Attributes
If a request includes body, then it will compared with the recorded request body to match with. If you want to exclude body attributes from the comparison, you can set ignoredRequestBodyAttributes
in cypress.json
.
{
"autorecord": {
"ignoredRequestBodyAttributes": ["token"]
}
}
How It Works
How does the recording and stubbing work?
Cypress Autorecord uses Cypress' built-in cy.intercept
to hook into every request, including GET, POST, DELETE and PUT. If no mocks exist for a test, the network requests and responses are captured and automatically written to a local file. If mocks exist for a test, each request response will be stubbed in the beforeEach
hook.
Where are the mocks saved?
The mocks will be automatically generated and saved in the /cypress/mocks/
folder. Mocks are grouped by test name and test file name. You will find mock files matching the name of your test files. Within your mock files, mocks are organized by test names in the order that they were called. Changing the test file name or test name will result to a disconnection to the mocks and trigger a recording on your next run.
Including test parents names
If more uniqueness or clearer naming is needed, then test parent names can also be included in test name.
{
"autorecord": {
"includeParentTestName": true
}
}
Create unique mock responses files
If json files are getting too large or more granularity is needed. Then specify seperateMockFiles in cypress.json:
{
"autorecord": {
"seperateMockFiles": true
}
}
For every unique request there will be a json file generated in /cypress/fixtures
folder. Requests are put into subfolders by request domain name. Every GET request with same pathname and query is considered same. For every other method types, request body and response objects are hashed. If same object always gets same hash.
Can I manually update the mocks?
Mocks are saved as a simple json object and can be updated manually. This is not recommended since any manual change you make will be overwritten when you automatically update the mocks. Leave the data management to cypress-autorecord. Make any modifications to the http calls inside your test so that it will be consistent across recordings.
it('should display an error message when send message fails', function () {
cy.intercept({
url: '/message',
method: 'POST',
status: 404,
response: { error: 'It did not work' },
});
cy.get('[data-cy="msgInput"]').type('Hello World!');
cy.get('[data-cy="msgSend"]').click();
cy.get('[data-cy="errorMessage"]').should(
'contain',
'Looks like we ran into a problem. Please try again.'
);
});
Known Issues
Uncaught CypressError appears for certain requests
You should only ever see this error when you have a test that calls the same url but expect different response bodies based on different request bodies. The error message will show up when your test fails but this error does not effect your mocks or tests in any way and is not causing your test to fail! If you are curious to know a little bit more, take a look here.
Contributions
I would really appreciate any help with bug fixes or any new features you think might be relevant! Feel free to submit a PR!