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

Angular Important Interview Topics

Composition vs Inheritance and Thinking in React.js

React Hooks With Google Fit API, a Perfect Javascript Marriage

10 things in JavaScript

Cinner the Conquerors — when our health campaign engages the athelete insides members

Offline-First Web Application Using Redux and PouchDB

Practice React Native Project for beginners

Using ng2-tel-input in Angular

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

Allowing Session to Connect to Oxen nodes

Session messaging app being connected to Oxen node

Essentials: Move your app to Huawei AppGallery with these simple steps!

My first application wireframe

Android Studio & HMS Core Kit Integration

Android Studio