Skip to content

fix: mock DB and assert no console.error in index route tests#145

Merged
mahata merged 1 commit intomainfrom
fix/mock-db-in-index-tests
Mar 26, 2026
Merged

fix: mock DB and assert no console.error in index route tests#145
mahata merged 1 commit intomainfrom
fix/mock-db-in-index-tests

Conversation

@mahata
Copy link
Copy Markdown
Owner

@mahata mahata commented Mar 26, 2026

Summary

  • Mock getDb in index.test.tsx via vi.mock("../db/index.js", ...), consistent with how channels.test.ts and messages.test.ts already mock the DB. The mock returns an empty array for the #general channel query, making the auto-join code path a clean no-op.
  • Add console.error spy that asserts console.error was never called during each test. This turns previously silent stderr noise into explicit test failures, catching regressions immediately.

Problem

The index route handler auto-joins authenticated users to #general by querying D1 via Drizzle ORM. In tests, testApp.ts sets c.env.DB = {} as D1Database (an empty object), so Drizzle queries fail with this.client.prepare is not a function. The route's try/catch swallowed the error via console.error(...), so tests passed with status 200 despite the DB error appearing in stderr.

Verification

  • pnpm test:run — 64 tests pass, zero stderr output
  • pnpm lint — clean
  • pnpm build:check — clean

The index route tests were passing despite DB errors because the route
handler's try/catch silently swallowed them. The test helper provided an
empty object as the D1 binding, causing Drizzle queries to fail with
'this.client.prepare is not a function'.

- Add vi.mock for ../db/index.js consistent with other route test files
- Add console.error spy that fails tests if unexpected errors occur
Copilot AI review requested due to automatic review settings March 26, 2026 22:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes noisy/hidden DB errors in the index route tests by properly mocking the DB layer and turning unexpected console.error output into explicit test failures, aligning the index tests with existing route test patterns.

Changes:

  • Mock getDb (and required schema exports) in index.test.tsx to prevent Drizzle/D1 runtime errors in test env.
  • Add a console.error spy per test and assert it was never called.

Comment on lines +40 to +41
expect(consoleErrorSpy).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afterEach asserts before calling mockRestore(). If the assertion fails, mockRestore() will never run, which can leak the console.error spy into subsequent tests and cause cascading failures. Consider always restoring first (e.g., capture consoleErrorSpy.mock.calls, restore in a finally, then assert on the captured calls).

Suggested change
expect(consoleErrorSpy).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
const consoleErrorCalls = consoleErrorSpy.mock.calls;
consoleErrorSpy.mockRestore();
expect(consoleErrorCalls.length).toBe(0);

Copilot uses AI. Check for mistakes.
@mahata mahata merged commit 975be36 into main Mar 26, 2026
7 checks passed
@mahata mahata deleted the fix/mock-db-in-index-tests branch March 26, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants