Last active
May 27, 2020 01:33
-
-
Save jamesfulford/a28b6b890e88f5022bcda5a73f51e396 to your computer and use it in GitHub Desktop.
Cypress POST handler template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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