Fix analytics-server test path resolution - #32
Merged
Conversation
The test's local Express app resolved docs/analytics.html via
`path.resolve(__dirname, "../../docs/analytics.html")`, which assumed
tests ran from dist/__tests__/. Vitest runs directly from src/__tests__/,
so the path was one level too shallow and sendFile returned 404.
Additionally, when running from a git worktree (where the repo lives
under `.claude/worktrees/...`), Express's `send` module rejects any path
containing a dot-prefixed segment by default (dotfiles: "ignore" -> 404),
independent of whether the file actually exists.
Fix:
- Resolve the HTML path from `process.cwd()` (vitest's project root)
instead of `__dirname`, so it works identically in source and built
layouts.
- Pass `{ dotfiles: "allow" }` to `res.sendFile` so the route works
when the repo sits under a dotfile directory (e.g. worktrees).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two tests in
src/__tests__/analytics-server.test.tswere failing pre-existing:GET /analytics (dashboard HTML) > returns HTML when analytics is enabledGET /analytics (dashboard HTML) > serves HTML without requiring a token (auth is client-side)Both expected status 200 but got 404.
Root cause
Two distinct issues, both in the test's local Express app's
/analyticsroute:__dirnamevscwdmismatch. The test resolved the HTML path viapath.resolve(__dirname, "../../docs/analytics.html"). That assumes__dirnameisdist/__tests__/, but vitest runs tests directly fromsrc/__tests__/, making the path one level too shallow (src/docs/analytics.html, which doesn't exist).Express
senddotfile denial. When the repo sits in a path containing a dot-prefixed segment (e.g./.claude/worktrees/agent-.../), Express's underlyingsendmodule returns 404 by default (dotfiles: "ignore") regardless of whether the file exists. This affects anyone running tests from a worktree under.claude/.Fix
process.cwd()(vitest's project root) rather than__dirname. Stable across source and built layouts.{ dotfiles: "allow" }tores.sendFileso the route serves files whose absolute path contains dot-prefixed segments.Test plan
npx vitest run src/__tests__/analytics-server.test.ts— 11/11 pass (was 9/11)npm test— 2368/2368 passnpm run build— cleannpx prettier --check "src/**/*.ts"— clean