Stubbing Responses With Cypress

FE and BE apps
POST /api/login HTTP/1.1
Accept: application/json
Content-Type: application/json
User-Agent: PostmanRuntime/7.28.4
Cache-Control: no-cache
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 65

{
"email": "samanpavel@test.io",
"password": "12345a"
}

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
x-powered-by: Express
vary: Origin, Accept-Encoding
access-control-allow-credentials: true
cache-control: no-store, no-cache, must-revalidate, proxy-revalidate
pragma: no-cache
expires: 0
surrogate-control: no-store
x-content-type-options: nosniff
content-type: application/json; charset=utf-8
content-length: 206
etag: W/"ce-yeRVTu3ODNSmqUtpXfm7BjGjoeE"
date: Sun, 21 Nov 2021 11:47:34 GMT
connection: close

{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InNhbWFucGF2ZWxAZ21haWwuY29tIiwiaWF0IjoxNjM3NDk1MjU0LCJleHAiOjE2Mzc0OTg4NTQsInN1YiI6IjEifQ.3vlfxdeOZaOG6yQ-aUjhKlS1lK5OdQ7Yu-6ei9g1JX4"
}
HTTP/1.1 500 Internal Server Error
cy
.fixture('credentials')
.then(user => {

cy
.intercept('POST', /api\/login/, {
statusCode: 500,
});

cy
.fillInLoginForm(user);

cy
.get('[data-cy=login-submit]')
.click();

cy
.contains('There was an error, please try later')
.should('be.visible');

cy
.url()
.should('match', /login$/);
});
cy
.intercept('POST', /api\/login/, {
statusCode: 500,
});
  • I want to test the frontend,
  • and it’s difficult to make the real server respond with a certain data or status code.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How we achieved 51% efficiency boost in button loading speed

React Currency Converter Application

Build an Agency Website with HTML & CSS -2

Building a real-time ASCII Camera Filter in plain Javascript

Lighthouse: improve performance scores by properly sizing images

Building a white-label apps using expo

Which equals operator should I use in JavaScript comparisons? (== vs ===)

Websockets, Long-Polling, and Server-Sent Events

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavel Saman

Pavel Saman

Tester

More from Medium

Challenges of Web UI Automation Testing

Class Blog Post

UI/UX Case Study-Redesigning Crowde Website

Deep Dive: Timeswap