diff --git a/packages/ui/node/index.ts b/packages/ui/node/index.ts index 1ae27b6f5bda..5ebda90f8c04 100644 --- a/packages/ui/node/index.ts +++ b/packages/ui/node/index.ts @@ -13,6 +13,7 @@ import { distClientRoot } from './paths' export { distClientRoot } const UI_TOKEN_COOKIE = 'vitest-ui-token' +const UI_TOKEN_COOKIE_MAX_AGE = 60 * 60 * 24 * 365 const AUTH_REQUIRED_MESSAGE = 'Vitest UI requires authentication. Open the URL with the token printed in the terminal, e.g. http://localhost:51204/__vitest__/?token=...' export default (harness: PluginHarness): Vite.Plugin => { @@ -40,6 +41,7 @@ export default (harness: PluginHarness): Vite.Plugin => { return serializeCookie(UI_TOKEN_COOKIE, ctx.config.api.token, { path: base, httpOnly: true, + maxAge: UI_TOKEN_COOKIE_MAX_AGE, sameSite: 'strict', }) } diff --git a/test/ui/test/ui.spec.ts b/test/ui/test/ui.spec.ts index 8b2bcfcd8b0a..fd37c51f03b7 100644 --- a/test/ui/test/ui.spec.ts +++ b/test/ui/test/ui.spec.ts @@ -110,6 +110,16 @@ test.describe('ui', () => { await assertTestCounts(page, { pass: TEST_COUNTS.pass, fail: TEST_COUNTS.fail }) expect(page.url()).toBe(`${cleanPageUrl}#/`) + const tokenCookie = (await page.context().cookies(cleanPageUrl)) + .find(cookie => cookie.name === 'vitest-ui-token') + expect(tokenCookie).toMatchObject({ + httpOnly: true, + sameSite: 'Strict', + }) + expect(tokenCookie!.expires).toBeGreaterThan( + Date.now() / 1000 + 60 * 60 * 24 * 364, + ) + await page.goto(cleanPageUrl) await assertTestCounts(page, { pass: TEST_COUNTS.pass, fail: TEST_COUNTS.fail }) expect(page.url()).toBe(`${cleanPageUrl}#/`)