Skip to content
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 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions packages/cypress/src/network-idle-watcher.test.ts
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();
});
42 changes: 42 additions & 0 deletions packages/cypress/src/network-idle-watcher.ts
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?.();
Copy link
Contributor

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.

}
}
}
Loading