-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(network shim): wait for network idle after tests
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/cypress-commands/src/setups/enableNetworkShim/waitForResources.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* copied and slightly modified from: | ||
* https://github.com/cypress-io/cypress/issues/1773#issuecomment-899684192 | ||
*/ | ||
|
||
let totalRunningQueries = 0 | ||
|
||
const observer = new PerformanceObserver(list => { | ||
for (const entry of list.getEntries()) { | ||
if (entry.initiatorType === 'fetch') { | ||
totalRunningQueries++ | ||
} | ||
} | ||
}) | ||
|
||
observer.observe({ | ||
entryTypes: ['resource'], | ||
}) | ||
|
||
Cypress.Commands.add('waitForResources', ({ maxTries = 3 } = {}) => { | ||
let tries = 0 | ||
|
||
return new Cypress.Promise(resolve => { | ||
const check = () => { | ||
const requests = window.performance | ||
.getEntriesByType('resource') | ||
.filter(n => n.initiatorType === 'fetch') | ||
|
||
if (requests.length === totalRunningQueries) { | ||
tries++ | ||
if (tries === maxTries) { | ||
resolve() | ||
} else { | ||
setTimeout(check, 100) | ||
} | ||
} else { | ||
tries = 0 | ||
setTimeout(check, 100) | ||
} | ||
} | ||
|
||
check() | ||
}) | ||
}) |