Skip to content

Commit

Permalink
fix: add example and document the timeout option (#182)
Browse files Browse the repository at this point in the history
* fix: add example and document the timeout option

* limit the release job
  • Loading branch information
bahmutov authored Jan 9, 2025
1 parent 86113fe commit 0f6de5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
release:
runs-on: ubuntu-20.04
needs: ['tests']
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout 🛎
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Semantic Release 🚀
uses: cycjimmy/semantic-release-action@v3
uses: cycjimmy/semantic-release-action@v4
with:
branches: main
env:
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ cy.waitForNetworkIdle('+(POST|GET)', '*', 5000)

For pattern matching see more examples in the [`cy.intercept()` documentation](https://docs.cypress.io/api/commands/intercept#Pattern-Matching).

## Timeouts

Let's take this example

```js
cy.waitForNetworkIdle('POST', '/graphql', 5000)
```

The `5000` argument is the wait number in milliseconds. The command will check for 5 seconds with _no_ `POST /graphql` calls. But how long do we need to wait for those idle 5 seconds? By default, it is the maximum of `Cypress.config('responseTimeout')` value (usually 30 seconds in Cypress) and the three times the wait amount (in our case 15 seconds).

You can set a longer time limit for waiting for network to be idle for 5 seconds. Let's wait for up to a minute for 5 second idle period:

```js
cy.waitForNetworkIdle('POST', '/graphql', 5000, { timeout: 60_000 })
```

## No logging

You can disable the log messages by adding option object with `{ log: false }` property
Expand Down
8 changes: 8 additions & 0 deletions cypress/e2e/spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="../../src/index.d.ts" />
// @ts-check

import '../..'

Expand Down Expand Up @@ -88,3 +89,10 @@ it('no logging', () => {
cy.waitForNetworkIdle('*', '/user', 1100, { log: false })
cy.window().its('user').should('deep.equal', { name: 'Test User' })
})

it('waits for up to a minute', () => {
cy.intercept('GET', '/user', { name: 'Test User' })
cy.visit('/')
cy.waitForNetworkIdle('*', '/user', 1100, { timeout: 60_000 })
cy.window().its('user').should('deep.equal', { name: 'Test User' })
})
8 changes: 8 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ declare namespace Cypress {
* @param method HTTP method to spy on
* @param pattern URL pattern to spy on
* @param waitMs Milliseconds after the last network call
* @param options Additional network options
*/
waitForNetworkIdle(
method: string,
Expand All @@ -137,6 +138,13 @@ declare namespace Cypress {
method?: string
pattern: string
alias: string
/**
* Max time limit for waiting for the idle network period, ms
* @example
* // wait for a 1-second idle period
* // max waiting time is 5 seconds
* cy.waitForNetworkIdle(1000, '*', { timeout: 5000 })
*/
timeout: number
interval: number
log?: boolean
Expand Down

0 comments on commit 0f6de5b

Please sign in to comment.