Skip to content

Instantly share code, notes, and snippets.

@bobmacneal
Last active December 23, 2019 14:00
Show Gist options
  • Save bobmacneal/3544d353da1be7f355fa5b5171fef8ed to your computer and use it in GitHub Desktop.
Save bobmacneal/3544d353da1be7f355fa5b5171fef8ed to your computer and use it in GitHub Desktop.
A function to write a Cypress.io fixture (for subsequent XHR mocking) via a "temporarily live" API request
describe('Cypress Request to JSON Writer', () => {
const JSON_PLACEHOLDER_API_URL = 'https://jsonplaceholder.typicode.com'
const FILENAME = 'users.json'
it('Writes XHR to a JSON fixture file', () => {
const url = `${JSON_PLACEHOLDER_API_URL}/users` // the API call
// Use cy.request to hit the API url. Add bearer token if auth required for API.
cy.request({
url: url,
auth: {
bearer: '',
},
}).then(
response => {
// use the response.body returned from API to write a fixture.
cy.writeFile(`cypress/fixtures/${FILENAME}`, response.body)
}
)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment