Skip to content

Add missing Jest unit tests for backend API routes and service layer#483

Open
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1776250226-add-missing-backend-tests
Open

Add missing Jest unit tests for backend API routes and service layer#483
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1776250226-add-missing-backend-tests

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot commented Apr 15, 2026

Summary

Adds 28 new Jest unit tests across 4 test files to improve backend test coverage from ~87% to ~98.7% statements. No production code changes.

New tests by file:

  • reports.test.js (+8 tests): PDF export success path — generation with entries, empty entries, page break when y > 700, separator lines every 5 entries, null description fallback, response headers. CSV export writer configuration verification.
  • clients.test.js (+10 tests): Bulk DELETE /api/clients, department/email field updates (including empty-string-to-null coercion), unexpected error handling in POST/PUT try-catch blocks.
  • database/init.test.js (+3 tests): closeDatabase edge cases — no existing connection, already-closed state, concurrent close calls (isClosing race condition).
  • workEntries.test.js (+2 tests): Unexpected error handling in POST/PUT try-catch blocks.

Remaining uncovered lines: reports.js:127-134 (the res.download callback + fs.unlink cleanup). This path cannot be reliably tested via supertest because jest.mock('fs') interferes with Express's internal res.download/sendFile implementation, causing the response to hang.

Review & Testing Checklist for Human

  • PDF mock approach correctness: PDF tests use a capturedRes pattern where pipe captures the Express response and end calls capturedRes.end() to unblock supertest. Verify this exercises the actual PDF generation code paths (lines 187-240 of reports.js) and not just the mock wiring.
  • CSV "writeRecords Success Path" is actually an error path: The test intentionally rejects writeRecords to avoid the res.download hang, so it validates csvWriter configuration but doesn't cover the download callback (lines 127-134). Verify the test name/comments are not misleading.
  • closeDatabase concurrent close test: Confirm the deferred-callback pattern (closeCallback stored then resolved later) actually exercises the isClosing branch in database/init.js rather than just the isClosed early return.
  • Run cd backend && npm run test:coverage locally and confirm 182 tests pass with 0 failures and coverage thresholds are met.

Notes

  • All tests are test-only additions (no production code modified), so risk of production regression is minimal.
  • The bulk delete tests for clients.test.js use callback.call(this, null) with this.changes to mimic sqlite3's callback context — worth a glance to confirm this matches the actual sqlite3 behavior the route depends on.

Link to Devin session: https://partner-workshops.devinenterprise.com/sessions/81daa0376237441192fea8e5830fddf3
Requested by: @VedantKh


Open with Devin

- Add 13 new tests for routes/reports.js: PDF generation (pipe/end, empty entries, page breaks, separator lines, null descriptions, response headers) and CSV export (writeRecords, error paths)
- Add 10 new tests for routes/clients.js: bulk delete, department/email field updates, unexpected error handling
- Add 3 new tests for database/init.js: closeDatabase edge cases (no-db, already closed, concurrent close)
- Add 2 new tests for routes/workEntries.js: unexpected error handling for create and update

Coverage improved from 87% to 98.69% statements overall.
Per-file coverage: clients.js 100%, workEntries.js 100%, auth.js 100%, init.js 100%, reports.js 94.33%
@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Copy link
Copy Markdown
Contributor Author

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

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.

0 participants