Skip to content

Instantly share code, notes, and snippets.

@jamesfulford
Last active May 27, 2020 01:33
Show Gist options
  • Select an option

  • Save jamesfulford/a28b6b890e88f5022bcda5a73f51e396 to your computer and use it in GitHub Desktop.

Select an option

Save jamesfulford/a28b6b890e88f5022bcda5a73f51e396 to your computer and use it in GitHub Desktop.
Cypress POST handler template
function handleRequestWith(makeResponse) {
return (rawRequest) => {
const { xhr } = rawRequest;
Object.defineProperty(xhr.__proto__, 'response', { writable: true });
xhr.response = JSON.stringify(makeResponse(rawRequest));
return rawRequest;
}
}
// sample usage:
cy.route({
url: '/api/heroes',
method: 'POST',
response: '', // this will be overwritten by handleRequestWith
onResponse: handleRequestWith(({ request }) => {
// TODO(you): Put your handling logic here!
const body = JSON.parse(request.body);
// Return the response's body
return { errorCode: 1337, message: "You hit the wrong button!" };
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment