#cypress-autostub This plugin for the Cypress automation framework alleviates the need to mantain brittle manual mocks by automating the recording and stubbing of requests.
yarn install cypress-autostub
Setup the plugin along with any desired global settings in cypress/plugins/index.js
// Import the plugin
import { autoStubSetup } from 'cypress-autostub'
module.exports = (on, config) => {
autoStubSetup(on, {
// Global settings go here
})
}
Import the stubbing/recording function in your spec files
example.spec.js
import { autoStub } from 'cypress-autostub'
describe('Some Feature', () => {
beforeEach(() => {
// Put it in a before each
autoStub()
})
it('should do the thing', () => {
// Or even at the test level
autoStub()
})
})
Recording is the process of listening to the network requests that are fired during the execution of your test, and writing the responses to disk.
Mock files are stored on disk in a format that can be directly plugged into cy.route
.
The default location for these mock responses to be stored is cypress/mocks
but this can be configured globally
The name of the mock file is tied to:
a: The currently executing spec b: The currently executing test
So if you are running the following test in SomePage/example.spec.js
:
describe('MyFeature', () => {
it('should do a really important thing')
})
The mock file will by default be stored at:
cypress/mocks/SomePage/example/should do a really important thing.json
Note: The spacing in the file name is something I was not entirely happy with , and may be subject to change (or be configurable)
Provided the currently executing spec file has called autoStub
, the plugin will begin recording if any of the following are true:
CYPRESS_FORCE_RECORD
is set in the environmentforceRecord: true
is set globallyforceRecord: true
is set locallyThere is no such thing as "cleaning" up your mock files. If a mock file already exists and the plugin is force recording. The previous mock file will be overwritten.
I believe this is the least error prone method of mantaining the mock files.
Stubbing is very simply calling:
cy.route()
With the data stored in the mock files used as arguments for the cy.route
Generated using TypeDoc