-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Network idle watcher for Cypress #173
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { NetworkIdleWatcher } from './network-idle-watcher'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
it('Resolves when there is no network activity', async () => { | ||
const watcher = new NetworkIdleWatcher(); | ||
await expect(watcher.idle()).resolves.toBeDefined(); | ||
}); | ||
|
||
it('Resolves when there is a single request and response', async () => { | ||
const watcher = new NetworkIdleWatcher(); | ||
|
||
watcher.onRequest(); | ||
watcher.onResponse(); | ||
|
||
await expect(watcher.idle()).resolves.toBeDefined(); | ||
}); | ||
|
||
it('Resolves when there are an equal amount of requests and responses', async () => { | ||
const watcher = new NetworkIdleWatcher(); | ||
// in total 4 requests, and 4 responses | ||
watcher.onRequest(); | ||
watcher.onResponse(); | ||
|
||
watcher.onRequest(); | ||
watcher.onResponse(); | ||
|
||
watcher.onRequest(); | ||
watcher.onRequest(); | ||
|
||
watcher.onResponse(); | ||
watcher.onResponse(); | ||
|
||
await expect(watcher.idle()).resolves.toBeDefined(); | ||
}); | ||
|
||
it('Rejects if response never sent for request', async () => { | ||
const watcher = new NetworkIdleWatcher(); | ||
// fire off request | ||
watcher.onRequest(); | ||
const promise = watcher.idle(); | ||
jest.runAllTimers(); | ||
// no response fired off | ||
await expect(promise).rejects.toBeDefined(); | ||
}); | ||
|
||
it("Resolves if response hasn't happened at time of idle(), but comes back before timeout", async () => { | ||
const watcher = new NetworkIdleWatcher(); | ||
// fire off request | ||
watcher.onRequest(); | ||
|
||
const promise = watcher.idle(); | ||
|
||
watcher.onResponse(); | ||
// makes sure we finish the idle watcher as soon as the reponse comes back, and not waiting the full timeout duration | ||
jest.advanceTimersByTime(1); | ||
|
||
await expect(promise).resolves.toBeDefined(); | ||
}); | ||
|
||
it("Rejects if response hasn't happened at time of idle(), and doesn't come back before timeout", async () => { | ||
const watcher = new NetworkIdleWatcher(); | ||
// fire off request | ||
watcher.onRequest(); | ||
|
||
const promise = watcher.idle(); | ||
|
||
// response returns after idle() has been called, but will take too long | ||
setTimeout(() => { | ||
watcher.onResponse(); | ||
}, 10000); | ||
|
||
jest.runAllTimers(); | ||
|
||
await expect(promise).rejects.toBeDefined(); | ||
}); |
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,42 @@ | ||
const TOTAL_TIMEOUT_DURATION = 3000; | ||
|
||
// A Cypress equivalent of Playwright's `page.waitForLoadState()` (https://playwright.dev/docs/api/class-page#page-wait-for-load-state). | ||
// Intentionally simplistic since in Cypress this is just used to make sure there aren't any pending requests hanging around | ||
// after the test has finished. | ||
export class NetworkIdleWatcher { | ||
private numInFlightRequests = 0; | ||
|
||
private idleTimer: NodeJS.Timeout | null = null; | ||
|
||
private exitIdleOnResponse: () => void | null = null; | ||
|
||
async idle() { | ||
return new Promise((resolve, reject) => { | ||
if (this.numInFlightRequests === 0) { | ||
resolve(true); | ||
} else { | ||
this.idleTimer = setTimeout(() => { | ||
reject(new Error('some responses have not returned')); | ||
}, TOTAL_TIMEOUT_DURATION); | ||
|
||
// assign a function that'll be called as soon as responses are all back | ||
this.exitIdleOnResponse = () => { | ||
resolve(true); | ||
}; | ||
} | ||
}); | ||
} | ||
|
||
onRequest() { | ||
this.numInFlightRequests += 1; | ||
} | ||
|
||
onResponse() { | ||
this.numInFlightRequests -= 1; | ||
// resolve immediately if the in-flight request amount is now zero | ||
if (this.numInFlightRequests === 0) { | ||
clearTimeout(this.idleTimer); | ||
this.exitIdleOnResponse?.(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, I didn't know you could invoke a function using optional chaining like this.