diff --git a/cypress/e2e/multiple-tests.js b/cypress/e2e/multiple-tests.js new file mode 100644 index 0000000..cb7f156 --- /dev/null +++ b/cypress/e2e/multiple-tests.js @@ -0,0 +1,40 @@ +/// +// @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) +}) diff --git a/src/index.js b/src/index.js index 131d65f..ddc2bda 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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)) {