Repository navigation
test: run frontend unit tests in browsers - #38860
Conversation
happy-dom does not compute layout and lacks ":scope" support, which forced test-only branches into production code in dom.ts. Running the suite in real Chromium removes those workarounds and exercises actual browser behavior instead of an approximation of it. Test isolation is restored because a shared module registry lets one file's vi.mock leak into another. In a browser that is fatal rather than cosmetic: an unapplied navigation mock lets the real location.assign run, which navigates the test page away and kills the runner. Since window.location cannot be stubbed at all in a real browser, the two affected call sites go through wrappers instead. Assisted-by: Claude Code:claude-opus-5
happy-domThe plugin already matches .txt, so the ?raw suffix only bypassed it in favour of vite's builtin and introduced a second raw-import convention. Assisted-by: Claude Code:claude-opus-5
Signed-off-by: silverwind <me@silverwind.io>
The plugin matched by file extension, so it was not visible at the call site whether an import went through it or through vite's builtin, and appending "?raw" silently bypassed it. Vite's own suffix states the intent per import and needs no hand-written module declarations. Emitted assets are unchanged. Assisted-by: Claude Code:claude-opus-5
Vitest 4 ships it inside every browser provider package, so declaring it here only pins a second copy of a version the provider already fixes. Assisted-by: Claude Code:claude-opus-5
A real browser makes window.location non-configurable, so tests cannot stub it. The first attempt routed production code through navigateTo and reloadPage wrappers purely so tests could intercept them. The Navigation API observes and cancels the navigation directly, so production code goes back to calling window.location and the interception lives entirely in the test helper. Intra-file concurrency is dropped because these tests observe shared global state: one window.navigation listener and one window.open spy. Assisted-by: Claude Code:claude-opus-5
Browser provisioning and e2e orchestration are separate concerns, and the unit tests need a browser now too, so tools/playwright.sh owns the install while tools/test-e2e.sh keeps the container lifecycle. Neither depends on the other. --with-deps is gated on the distro as well as CI, so forcing local mode on a host without apt-get no longer fails. Assisted-by: Claude Code:claude-opus-5
vite/client already declares it, and it is in tsconfig types. The local copy also declared a default string export that vite only provides for ".css?inline", so a broken default import would have type-checked. Assisted-by: Claude Code:claude-opus-5
|
Ready now, playwright's browser installation is now shared between e2e and test-frontend and the tests use https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API instead of overwriting |
The args array only ever held one optional flag, and it had to be seeded with "install" because bash 3.2 on macOS aborts on an empty array under set -u. Branching on the whole command is shorter and needs no shellcheck exception for an unquoted expansion. Assisted-by: Claude Code:claude-opus-5
The split into tools/playwright.sh dropped the variable. It comes back as positional arguments forwarded by the Makefile, so the flags stay quoted and need no shellcheck exception for word splitting. Assisted-by: Claude Code:claude-opus-5
There was a problem hiding this comment.
Pull request overview
Switches the frontend unit test environment from happy-dom to running in a real browser via Vitest’s browser mode (Playwright provider), and updates the code/tests/tooling to work in that environment. This also removes vite-string-plugin by standardizing on Vite’s native ?raw imports for SVG/text assets.
Changes:
- Run
web_src/**/*.test.tsin Vitest browser mode (Playwright/Chromium) and keeptools/**/*.test.tsin Node. - Replace
vite-string-pluginusage with?rawimports for SVG/text fixtures. - Update affected unit tests and scripts (navigation expectations, DOM/layout behavior, Playwright install flow).
Reviewed changes
Copilot reviewed 22 out of 24 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| web_src/js/webcomponents/overflow-menu.ts | Switch SVG import to ?raw for browser-mode compatibility. |
| web_src/js/vitest.setup.ts | Derive appUrl from the actual test page origin. |
| web_src/js/utils/testhelper.ts | Add navigation capture helper for browser-mode tests. |
| web_src/js/utils/match.test.ts | Simplify mock factory formatting. |
| web_src/js/utils/glob.test.ts | Replace Node file read with ?raw fixture import. |
| web_src/js/utils/dom.ts | Remove happy-dom-specific workarounds and visibility special-casing. |
| web_src/js/utils/dom.test.ts | Update visibility test to rely on real document layout. |
| web_src/js/svg.ts | Convert SVG imports to ?raw for Vite-native raw loading. |
| web_src/js/render/ansi.test.ts | Update expectations to match browser-normalized CSS serialization (rgb(...)). |
| web_src/js/modules/errors.test.ts | Make origin-dependent assertions robust in browser mode. |
| web_src/js/markup/render-iframe.test.ts | Replace window.location stubbing with navigation capture. |
| web_src/js/features/repo-settings-branches.test.ts | Simplify mocks and make assertions more direct. |
| web_src/js/features/common-fetch-action.test.ts | Replace window.location stubbing with navigation capture and table-driven cases. |
| vitest.config.ts | Split tests into browser and node projects; enable Playwright provider for browser tests. |
| vite.config.ts | Remove vite-string-plugin from build pipeline. |
| types.d.ts | Drop custom *.svg/*.css module declarations (rely on Vite types). |
| tools/test-e2e.sh | Simplify Playwright container handling and remove install subcommand. |
| tools/shared.ts | Remove vite-string-plugin from shared Vite plugin list. |
| tools/playwright.sh | Add a dedicated Playwright install helper script. |
| pnpm-lock.yaml | Update lockfile for dependency removals/additions (Playwright/Vitest browser provider). |
| package.json | Remove happy-dom/vite-string-plugin; add @vitest/browser-playwright. |
| Makefile | Wire test-frontend to Playwright install; update e2e invocation. |
| docs/testing.md | Document that frontend unit tests use Vitest browser mode. |
| .github/workflows/files-changed.yml | Update file-change routing to reflect new tooling/scripts. |
Files not reviewed (1)
- pnpm-lock.yaml: Generated file
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The vitest browser project is chromium-only, so make test-frontend no longer pulls firefox. The playwright target keeps both for the e2e suite, which runs chromium and firefox projects. Narrow the frontend path filter to the tools files the frontend actually consumes, so it still catches shared.ts and eslint-rules but no longer triggers the frontend and e2e jobs on go-only tooling changes. Assisted-by: Claude Code:claude-opus-5
…able Reverts the per-target browser split in favour of the simpler shared prerequisite. PLAYWRIGHT_BROWSERS and PLAYWRIGHT_FLAGS keep the browser set and install flags overridable without a second target. Assisted-by: Claude Code:claude-opus-5
Matches the e2e job, so the browser download stays out of the test step and its output shows only test results. Assisted-by: Claude Code:claude-opus-5
The captured navigation list already orders and counts the cases, so a table adds indirection without adding coverage. Assisted-by: Claude Code:claude-opus-5
Keeps the diff focused on the test environment switch. The plugin change was an unrelated cleanup. The glob fixture still imports its data instead of reading it with node:fs, since the browser has no fs, so ".txt" keeps a module declaration. Assisted-by: Claude Code:claude-opus-5
vite/client already declares it, and it is in tsconfig types. The local copy also declared a default string export that vite only provides for ".css?inline", so a broken default import would have type-checked. Assisted-by: Claude Code:claude-opus-5
Derive the instances from `PLAYWRIGHT_BROWSERS` so that the browsers being installed and the ones being run cannot diverge. Co-Authored-By: Claude (Opus 5) <noreply@anthropic.com>
|
Now it runs in Firefox too, same as the e2e tests. Test runtime is now 5s on my machine. Disabling |
Signed-off-by: silverwind <me@silverwind.io>
* 'main' of https://github.com/go-gitea/gitea: (29 commits) fix(actions): Fix how jobs in matrixes are grouped (go-gitea#38980) enhance(ui): forced colors mode enhancements (go-gitea#38991) fix: resolve YAML anchors and aliases in Actions workflows (go-gitea#38984) [skip ci] Updated translations via Crowdin fix(lfs): ensure lock listing paginates with a total order (go-gitea#38850) fix: resolve actions commit status permission per repository (go-gitea#38977) chore(deps): update dependency go to v1.26.7 (go-gitea#38987) chore: form binding trim space (go-gitea#38978) fix: honor environment variables during install (go-gitea#38974) refactor: http request binding (go-gitea#38971) [skip ci] Updated translations via Crowdin enhance(ui): tint toast backgrounds by level (go-gitea#38919) ci: improve caching (go-gitea#38958) refactor: private endpoints (go-gitea#38964) fix: grant limited-org unit read access to authenticated non-members (go-gitea#38871) fix: allow anonymous theme switching when REQUIRE_SIGNIN_VIEW is set (go-gitea#38956) [skip ci] Updated translations via Crowdin fix(actions): drop wrapper span around the action status icon (go-gitea#38957) test: run frontend unit tests in browsers (go-gitea#38860) chore(maintainers): add wingsallen as maintainer (go-gitea#38913) ... # Conflicts: # MAINTAINERS
* main: (29 commits) fix(actions): Fix how jobs in matrixes are grouped (go-gitea#38980) enhance(ui): forced colors mode enhancements (go-gitea#38991) fix: resolve YAML anchors and aliases in Actions workflows (go-gitea#38984) [skip ci] Updated translations via Crowdin fix(lfs): ensure lock listing paginates with a total order (go-gitea#38850) fix: resolve actions commit status permission per repository (go-gitea#38977) chore(deps): update dependency go to v1.26.7 (go-gitea#38987) chore: form binding trim space (go-gitea#38978) fix: honor environment variables during install (go-gitea#38974) refactor: http request binding (go-gitea#38971) [skip ci] Updated translations via Crowdin enhance(ui): tint toast backgrounds by level (go-gitea#38919) ci: improve caching (go-gitea#38958) refactor: private endpoints (go-gitea#38964) fix: grant limited-org unit read access to authenticated non-members (go-gitea#38871) fix: allow anonymous theme switching when REQUIRE_SIGNIN_VIEW is set (go-gitea#38956) [skip ci] Updated translations via Crowdin fix(actions): drop wrapper span around the action status icon (go-gitea#38957) test: run frontend unit tests in browsers (go-gitea#38860) chore(maintainers): add wingsallen as maintainer (go-gitea#38913) ...
…39905) On Debian/Ubuntu outside GitHub Actions, `tools/playwright.sh` always ran `pnpm exec playwright install --with-deps`. Installing system packages needs root: when it isn't root, Playwright tries `sudo` and falls back to `su`, so a non-root user without passwordless sudo sees `make playwright` — and the targets depending on it, `make test-e2e` and `make test-frontend` — either stop and ask for the root password mid-build or fail outright with `su: System error`. Related: #38860, which added this script. --------- Co-authored-by: silverwind <me@silverwind.io>
Run them in headless vitest browser mode in chromium and firefox. Similar UX than current tests, it's about 5 times as slow (goes from 1s to 5s on my machine), but definitely worth it as it removes all happy-dom problems.