Skip to content

Commit 2aa76b0

Browse files
authored
test(browser): Fix flaky loader test (#20596)
Ironically, this flaked in a PR fixing another flake. Closes #20564
1 parent 1bc267d commit 2aa76b0

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

  • dev-packages/browser-integration-tests/loader-suites/loader/noOnLoad/sdkLoadedInMeanwhile

dev-packages/browser-integration-tests/loader-suites/loader/noOnLoad/sdkLoadedInMeanwhile/subject.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
setTimeout(() => {
22
const cdnScript = document.createElement('script');
3-
cdnScript.src = '/cdn.bundle.js';
3+
// Distinct URL from the loader's `/cdn.bundle.js` so Chromium cannot satisfy this via memory-cache
4+
// (would skip `page.route` and make CDN load counts flaky).
5+
cdnScript.src = `/cdn.bundle.js?sentryInjected=1`;
46

57
cdnScript.addEventListener('load', () => {
68
Sentry.init({

dev-packages/browser-integration-tests/loader-suites/loader/noOnLoad/sdkLoadedInMeanwhile/test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ sentryTest('it does not download the SDK if the SDK was loaded in the meanwhile'
3030
const tmpDir = await getLocalTestUrl({ testDir: __dirname, skipRouteHandler: true, skipDsnRouteHandler: true });
3131

3232
await page.route(`${TEST_HOST}/*.*`, route => {
33-
const file = route.request().url().split('/').pop();
33+
const pathname = new URL(route.request().url()).pathname;
34+
const file = pathname.split('/').pop() || '';
3435

36+
// Loader + subject both fetch the CDN bundle. Chromium may not hit `page.route` twice for the same URL
37+
// (memory cache); subject.js uses a cache-busted URL so we reliably observe two network loads.
3538
if (file === 'cdn.bundle.js') {
3639
cdnLoadedCount++;
3740
}
@@ -47,10 +50,8 @@ sentryTest('it does not download the SDK if the SDK was loaded in the meanwhile'
4750

4851
const eventData = envelopeRequestParser(req);
4952

50-
await waitForFunction(() => cdnLoadedCount === 2);
51-
5253
// Still loaded the CDN bundle twice
53-
expect(cdnLoadedCount).toBe(2);
54+
await expect.poll(() => cdnLoadedCount, { timeout: 15_000 }).toBe(2);
5455

5556
// But only sent to Sentry once
5657
expect(sentryEventCount).toBe(1);
@@ -62,10 +63,3 @@ sentryTest('it does not download the SDK if the SDK was loaded in the meanwhile'
6263
expect(eventData.exception?.values?.length).toBe(1);
6364
expect(eventData.exception?.values?.[0]?.value).toBe('window.doSomethingWrong is not a function');
6465
});
65-
66-
async function waitForFunction(cb: () => boolean, timeout = 2000, increment = 100) {
67-
while (timeout > 0 && !cb()) {
68-
await new Promise(resolve => setTimeout(resolve, increment));
69-
await waitForFunction(cb, timeout - increment, increment);
70-
}
71-
}

0 commit comments

Comments
 (0)