Skip to content

Commit 8c6328c

Browse files
test(mosaic): clear authentication cookies when test iframes unload
1 parent 3c235e0 commit 8c6328c

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,6 @@ jobs:
426426
run: pnpm turbo build $TURBO_ARGS --filter=@clerk/mosaic...
427427

428428
- name: Run feature tests
429-
env:
430-
VITEST_BROWSER_DEBUG: 'true'
431-
VITEST_PW_DEBUG: '1'
432-
DEBUG: 'vitest:browser:gc'
433429
run: pnpm --filter @clerk/mosaic test:feature
434430

435431
- name: Run browser tests

‎packages/mosaic/src/__tests__/feature/fake-fapi-isolation.feature.test.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ describe('fake FAPI isolation between tests', () => {
4444
expect(await clerk.session?.getToken()).toEqual(expect.any(String));
4545
expect(document.cookie).not.toMatch(/(?:^|; )__session/);
4646
});
47+
48+
it('clears authentication cookies when the test iframe leaves the page', async () => {
49+
const user = fapiUser({ id: 'user_1' });
50+
serveFapi({ client: fapiClient([fapiSession({ id: 'sess_1', user })]) });
51+
await renderWithClerk(<div />, { standardBrowser: true });
52+
window.dispatchEvent(new Event('focus'));
53+
await new Promise(resolve => setTimeout(resolve, 0));
54+
expect(document.cookie).toContain('__session=');
55+
56+
window.dispatchEvent(new PageTransitionEvent('pagehide'));
57+
58+
expect(document.cookie).toBe('');
59+
});
4760
});

‎packages/mosaic/vitest.setup.browser.mts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,21 @@ beforeEach(() => {
6464
window.focus();
6565
});
6666

67+
function clearCookies() {
68+
for (const cookie of document.cookie.split('; ').filter(Boolean)) {
69+
document.cookie = `${cookie.split('=')[0]}=; max-age=0; path=/`;
70+
}
71+
}
72+
73+
window.addEventListener('pagehide', clearCookies);
74+
6775
afterEach(() => {
6876
cleanup();
6977
__resetClerkQueryClientForTest();
7078
for (const channel of [...openChannels]) {
7179
channel.close();
7280
}
73-
for (const cookie of document.cookie.split('; ').filter(Boolean)) {
74-
document.cookie = `${cookie.split('=')[0]}=; max-age=0; path=/`;
75-
}
81+
clearCookies();
7682
localStorage.clear();
7783
sessionStorage.clear();
7884
worker.resetHandlers();

0 commit comments

Comments
 (0)