Skip to content

Commit

Permalink
fix: reset all counters after each test (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 16, 2022
1 parent 3125400 commit 6e226b4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cypress/e2e/multiple-tests.js
@@ -0,0 +1,40 @@
/// <reference path="../../src/index.d.ts" />
// @ts-check

import '../..'

// test for multiple registrations
// https://github.com/bahmutov/cypress-network-idle/issues/54

it('stubs test 1', () => {
cy.visit('/button')
cy.waitForNetworkIdlePrepare({
method: '*',
pattern: '/user',
alias: 'user',
})
cy.get('#fetch').click()
cy.waitForNetworkIdle('@user', 1000).its('callCount').should('equal', 1)
})

it('stubs test 2', () => {
cy.visit('/button')
cy.waitForNetworkIdlePrepare({
method: '*',
pattern: '/user',
alias: 'user',
})
cy.get('#fetch').click()
cy.waitForNetworkIdle('@user', 1000).its('callCount').should('equal', 1)
})

it('stubs test 3', () => {
cy.visit('/button')
cy.waitForNetworkIdlePrepare({
method: '*',
pattern: '/user',
alias: 'user',
})
cy.get('#fetch').click()
cy.waitForNetworkIdle('@user', 1000).its('callCount').should('equal', 1)
})
14 changes: 14 additions & 0 deletions src/index.js
Expand Up @@ -2,6 +2,18 @@

const logPrefix = '**network-idle**'

// reset all counters after every test
// because Cypress resets all intercepts
// https://github.com/bahmutov/cypress-network-idle/issues/54
Cypress.on('test:after:run', () => {
const env = Cypress.env()
Cypress._.each(env, (value, key) => {
if (key.startsWith('networkIdleCounters_')) {
delete env[key]
}
})
})

function waitForIdle(counters, timeLimitMs, timeout, interval) {
counters.started = +new Date()
counters.finished = null
Expand Down Expand Up @@ -177,6 +189,8 @@ function waitForNetworkIdlePrepare({ method, pattern, alias, log } = {}) {
log = true
}

Cypress.log({ name: 'network-idle', message: `prepared for **@${alias}**` })

const key = `networkIdleCounters_${alias}`
// check if the intercept has already been set
if (Cypress.env(key)) {
Expand Down

0 comments on commit 6e226b4

Please sign in to comment.