Files
HealthLog/e2e/locale-switch.spec.ts
Marc-André Bombeck a23ee61184 test(e2e): route cleanup happens by construction, not per spec
A spec that arms `page.route(...)` in a `beforeEach` and never drops the
handler races its own teardown. The context closes, an async handler is
still awaiting, its next call rejects with "Test ended.", and Playwright
attributes that to the test that had just passed. With `retries: 2` plus
`failOnFlakyTests`, which spec bites is a matter of timing.

`e2e/setup/test.ts` re-exports Playwright's `test` extended with an auto
fixture that unroutes every page and the context after each test, and
clears the page event listeners a spec subscribes to; `expect` rides
along so a spec needs one import. Auto means a spec author cannot forget
it and does not have to know it exists. Every spec now takes `test` from
there, and `e2e-route-cleanup-guard.test.ts` keeps the import boundary
closed with an allowlist that carries its reason.

The per-file `afterEach` on the cycle spec goes away — leaving both would
read as not trusting the fixture.

Not covered: a page inside a context a spec builds itself. Only
`share-documents.spec.ts` does that, and it owns those contexts.
2026-07-25 23:02:03 +02:00

55 lines
2.0 KiB
TypeScript

import { expect, test } from "./setup/test";
/**
* Locale-key drift smoke check — switch the cookie between EN and DE
* and assert the login page (the only public surface that's translated)
* never shows raw i18n keys like `auth.welcomeBack`.
*
* Drift catches: a typo'd t("foo.bar") with no key in either locale
* file would make the raw key surface in the DOM. messages/de.json and
* messages/en.json key parity is enforced by unit tests; this is the
* runtime cousin.
*/
test.describe("locale switch", () => {
for (const locale of ["en", "de"] as const) {
test(`renders /auth/login in ${locale} without raw i18n keys`, async ({
page,
context,
}) => {
// Cookies must be addressable to a real origin — `page.url()` on
// a fresh context returns `about:blank`, which Playwright rejects.
// Anchor at the configured base URL explicitly.
const baseURL =
test.info().project.use.baseURL ?? "http://localhost:3000";
await context.addCookies([
{
name: "healthlog-locale",
value: locale,
url: baseURL,
},
]);
await page.goto("/auth/login");
const bodyText = await page.locator("body").innerText();
// i18n keys look like `section.subkey` — letters, numbers,
// underscores, dots — and are never wrapped in normal sentences,
// so a regex match anywhere in body text is a sign the lookup
// fell through.
const rawKeyPattern = /\b[a-z]+(?:[A-Z][a-z]+)?\.[a-z][A-Za-z0-9_.]+\b/;
const match = bodyText.match(rawKeyPattern);
// Whitelist: filenames or version strings can look like keys,
// skip those by requiring at least one camelCase segment.
if (match) {
const candidate = match[0];
const looksLikeKey = /\.[a-z][A-Z]/.test(candidate);
expect(
looksLikeKey,
`Possible raw i18n key in ${locale}: ${candidate}`,
).toBe(false);
}
});
}
});