Skip to content

Commit

Permalink
fix(network shim): wait for network idle after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Nov 9, 2021
1 parent 6c705b2 commit d025422
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './waitForResources.js'
import { setDhis2BaseUrlToLocalStorage } from '../../helper/dhis2BaseUrl.js'
import {
isLiveMode,
Expand Down Expand Up @@ -41,6 +42,8 @@ export function enableNetworkShim() {

afterEach(() => {
if (!isLiveMode()) {
cy.waitForResources()

// First get the updated local state from the alias
cy.get('@networkShimState').then(networkShimState => {
/*
Expand Down
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()
})
})

0 comments on commit d025422

Please sign in to comment.