I have a very large API where every path returns a unique resource value, and each time I get the resource value for a particular path it's always the same. Saving each response into a fixture file would take days and days.
I would like to use my application code to retrieve the API data, and I want Cypress to save the API responses to the fixture folder.
I then want to use the fixtures to intercept calls to the API during application runs where I'm testing and want the app code to run faster. Fixtures will be returned instead of waiting for the API to speed up the tests.
describe('fixture creation', () => {
it('should save fixtures of every endpoint that matches a url', () => {
// @ts-ignore
cy.login()
cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**', saveResponse)
cy.visit('/ubmc/system/pci-topology')
})
})
How do I write saveResponses to store the json responses from the endpoints that match the url pattern above?
const saveResponse = req => {
req.continue((res) => {
console.log('saved a file', `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`)
cy.writeFile(
`cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`,
res.body)
})
}
I get the "saved a file ..." console logs, but I checked the fixtures directory and it's empty. There are no errors, and placing a debugger statement inside the res handler doesn't tell me anything meaningful.