Skip to content

Commit

Permalink
feat: add fail on 4xx option (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Feb 1, 2023
1 parent 9b639a9 commit afa8cf4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,27 @@ cy.waitForNetworkIdle('@graphql', 1000)
// the page has finished additional processing
```

### fail on 5xx
### fail on error status code

By default, the network calls might fail and the test happily continues. You can make the idle spy fail if any of the matching network calls return 4xx or 5xx errors. These classes of error status code have their own flag to enable.

### fail on 4xx

```js
// fail the test if any of the matching calls
// returns a 4xx status code
cy.waitForNetworkIdlePrepare({
method: '*',
alias: 'all',
pattern: '**',
failOn4xx: true,
})
```

![The test fails when one of the calls receives 401 from the server](./images/4xx.png)

### fail on 5xx

```js
// fail the test if any of the matching calls
// returns a 5xx status code
Expand Down
32 changes: 32 additions & 0 deletions cypress/e2e/fail-on-400.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="../../src/index.d.ts" />
// @ts-check

import '../../src'

// fails the test on 401 response code
it('fails the test when a request receives 401 status code', () => {
cy.on('fail', (e) => {
if (e.message.includes('failed with 401')) {
console.log('expected error')
return false
} else {
throw e
}
})

// fail the test if any of the matching calls
// returns a 4xx status code
cy.waitForNetworkIdlePrepare({
method: '*',
alias: 'all',
pattern: '**',
failOn4xx: true,
})

cy.visit('/fail-500')
.wait(2000)
.then(() => {
// the network should have caught an 5xx error
throw new Error('Should never get here')
})
})
Binary file added images/4xx.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ declare namespace Cypress {
pattern: string
alias: string
log?: boolean
/**
* Fail the test if any of the matching network calls
* returns 4xx status code
*/
failOn4xx?: boolean
/**
* Fail the test if any of the matching network calls
* returns 5xx status code
Expand Down
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function waitForNetworkIdlePrepare({
alias,
log,
failOn5xx,
failOn4xx,
} = {}) {
if (!alias) {
throw new Error('cypress-network-idle: alias is required')
Expand All @@ -196,7 +197,11 @@ function waitForNetworkIdlePrepare({
}

let message = `prepared for **@${alias}**`
if (failOn5xx) {
if (failOn5xx && failOn4xx) {
message += ' (will fail on **4xx, 5xx**)'
} else if (failOn4xx) {
message += ' (will fail on **4xx**)'
} else if (failOn5xx) {
message += ' (will fail on **5xx**)'
}
Cypress.log({ name: 'network-idle', message })
Expand Down Expand Up @@ -237,6 +242,11 @@ function waitForNetworkIdlePrepare({
counters.lastNetworkAt = +new Date()
// console.log('res %s %s', req.method, req.url, counters.lastNetworkAt)
// console.log(res.body)
if (failOn4xx && res.statusCode >= 400 && res.statusCode < 500) {
throw new Error(
`Network call ${req.method} ${req.url} failed with ${res.statusCode}`,
)
}
if (failOn5xx && res.statusCode >= 500) {
throw new Error(
`Network call ${req.method} ${req.url} failed with ${res.statusCode}`,
Expand Down

0 comments on commit afa8cf4

Please sign in to comment.