fix: mock DB and assert no console.error in index route tests#145
Merged
fix: mock DB and assert no console.error in index route tests#145
Conversation
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
Contributor
There was a problem hiding this comment.
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) inindex.test.tsxto prevent Drizzle/D1 runtime errors in test env. - Add a
console.errorspy per test and assert it was never called.
Comment on lines
+40
to
+41
| expect(consoleErrorSpy).not.toHaveBeenCalled(); | ||
| consoleErrorSpy.mockRestore(); |
There was a problem hiding this comment.
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); |
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
getDbinindex.test.tsxviavi.mock("../db/index.js", ...), consistent with howchannels.test.tsandmessages.test.tsalready mock the DB. The mock returns an empty array for the#generalchannel query, making the auto-join code path a clean no-op.console.errorspy that assertsconsole.errorwas 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
#generalby querying D1 via Drizzle ORM. In tests,testApp.tssetsc.env.DB = {} as D1Database(an empty object), so Drizzle queries fail withthis.client.prepare is not a function. The route'stry/catchswallowed the error viaconsole.error(...), so tests passed with status 200 despite the DB error appearing in stderr.Verification
pnpm test:run— 64 tests pass, zero stderr outputpnpm lint— cleanpnpm build:check— clean