How do you test negative scenarios: 400, 401, 404?
By default cy.request fails on any status that is not 2xx or 3xx, so negative tests set failOnStatusCode: false. The expected code is then checked explicitly: 400 or 422 for invalid data, 401 without authentication, 403 without rights, 404 for a missing resource. Besides the code, the error body (no internal details) and the absence of side effects are checked. Without an expect on the status, the test passes on any response.
Cypress · API testing
A “401 without a token” test fails with a red 401, although that is exactly what it was meant to see. Where is the mistake?
A newcomer writes a negative test: a request with no token, expecting 401. They run it and Cypress shows red: cy.request() failed because the response had the status code 401. The test was right in intent, but it failed on the command, before any check. This is one of the most common questions about cy.request, and behind it stands a default you have to know.
How it works and why
By default failOnStatusCode is true: any status that is not 2xx or 3xx counts as a failure of the command itself. The logic is clear: in a positive test a 500 response should fail the test at once, without a separate expect. But a negative test expects 4xx. So you set failOnStatusCode: false, and then a response of any status is handed to the test as a normal value. You then check the status yourself, and it is easy to forget: without an expect on the status the test passes on any response.
One request, two behaviours
failOnStatusCode: true
A 400 or 404 response fails the test right on the command. Your expect never even runs.
failOnStatusCode: false
The response is handed to the test and you check the expected code, body and side effects yourself.
Example: three negative cases
Each case is a separate test with a clear expected code:
const noFail = { failOnStatusCode: false };
it('400: an empty order', () => {
cy.request({ method: 'POST', url: '/api/orders', body: { items: [] }, ...noFail })
.then((res) => {
expect(res.status).to.eq(400);
expect(res.body.message).to.contain('items');
});
});
it('401: no token', () => {
cy.request({ url: '/api/orders', ...noFail })
.its('status').should('eq', 401);
});
it('404: a missing resource', () => {
cy.request({ url: '/api/products/999999', ...noFail })
.its('status').should('eq', 404);
});
Checking only the status is not enough. A negative request must be safe, so note two things. First: the error body. It must be clear and carry no internals (a stack trace, SQL). Second: side effects. A failed creation must not leave a record, and that is visible only by reading again:
it('400 does not create an order', () => {
cy.request('/api/orders').its('body.length').then((before) => {
cy.request({
method: 'POST', url: '/api/orders', body: { items: [] },
failOnStatusCode: false,
}).its('status').should('eq', 400);
// the negative request changed nothing
cy.request('/api/orders').its('body.length').should('eq', before);
});
});
it('302 without following the redirect', () => {
cy.request({ url: '/old', followRedirect: false })
.its('headers.location').should('eq', '/new');
});
The second example shows one more option: followRedirect: false stops the automatic jump, so you can check the 302 response itself and the Location header. Typical expectations: 400 or 422 for invalid data (the team should pick one and stick to it), 401 without authentication, 403 without rights, 404 for a missing resource, 409 for a conflict.
Limits and alternatives
There is a neighbouring option, retryOnStatusCodeFailure: when it is on, Cypress repeats the request after a status failure (off by default). Negative tests do not need it. Do not mix the two ideas: failOnStatusCode: false leaves the response to the test, while retryOnStatusCodeFailure repeats the request. In Playwright the behaviour is the opposite: request.get() does not fail on 4xx and 5xx, you check the status yourself with res.status() or toBeOK(); and the failOnStatusCode: true option turns on failing, as in Cypress.
Typical mistakes
- Forgetting
failOnStatusCode: false: the test fails on the command beforeexpect. - Setting
falseand not checking the status: the test passes on any response, even 200. - Checking only the code and skipping the error body and side effects.
- Mixing up 401 and 403: the first means “not authenticated”, the second “authenticated but without rights”.
- Setting
failOnStatusCode: falseeverywhere “just in case”: positive tests lose their fast failure.
Check yourself
A test with failOnStatusCode: false on a request meant to return 404 passes, but there is no expect on the status anywhere. Why is that a problem?
Answer
Without the option the command would fail on 4xx itself, and now it fails on no status at all. If the status is not checked, the test passes on 200 and on 500, so it proves nothing.
Besides the status, what else is worth checking in a negative creation test?
Answer
The error body (a clear message with no internals) and the absence of side effects: a repeated read must show that no record was created.