Skip to content

Test: add vitest tests for GET and PUT /api/profiles/me routes - #76

Merged
ShantKhatri merged 2 commits into
Dev-Card:mainfrom
Dipti45sktech:profile
May 16, 2026
Merged

Test: add vitest tests for GET and PUT /api/profiles/me routes#76
ShantKhatri merged 2 commits into
Dev-Card:mainfrom
Dipti45sktech:profile

Conversation

@Dipti45sktech

Copy link
Copy Markdown
Contributor

Summary

Picked up issue #3 to add proper Vitest tests for the authenticated GET and PUT /api/profiles/me endpoints. Reviewed profiles.ts to understand how the user ID is extracted from JWT and how Prisma queries are handled.
Also checked validators.ts, focusing on the accentColor zod validation for correct failure cases.Set up a reusable buildApp() helper that creates a Fastify instance with Prisma and auth mocked using vi.fn(). This avoids needing a real database while keeping tests close to real behavior.
Wrote 5 test cases covering all required scenarios. These include successful profile fetch and update flows. Handled edge cases like 404 for missing user and 409 for duplicate username. Also added a 400 test for invalid accentColor input.
Ensured sensitive fields like provider and providerId are not returned in responses.
All tests pass locally.

Closes #3

Type of Change

  • Bug fix
  • New feature
  • Refactor (no functional change)
  • UI / Design change
  • Tests only
  • Documentation
  • Infrastructure / DevOps
  • Security

What Changed

  • Created apps/backend/src/__tests__/profiles.test.ts with full test coverage
  • Mocked app.prisma.user.findUnique, findFirst, and update using vi.fn()
  • Added a buildApp() helper that spins up a test Fastify instance with mocked auth and prisma
  • Covered GET happy path, GET 404, PUT success, PUT 400 (invalid accentColor), and PUT 409 (duplicate username)

How to Test

  1. Clone the repo and install dependencies with pnpm install
  2. Run pnpm --filter @devcard/backend test
  3. All 5 tests should pass with no errors

Checklist

  • My code follows the project's coding style (pnpm -r run lint passes).
  • TypeScript compiles without errors (pnpm -r run typecheck).
  • I have added or updated tests for the changes I made.
  • All tests pass locally (pnpm -r run test).
  • I have updated documentation where necessary.
  • No new console.log or debug statements left in the code.
  • Breaking changes are documented in this PR description.

Additional Context

Prisma and authentication are fully mocked so no database setup is needed to run these tests. The buildApp() helper can be reused in future test files for other routes.

@Dipti45sktech

Copy link
Copy Markdown
Contributor Author

Hi @ShantKhatri, would really appreciate if you could review my PR whenever available.

Comment thread apps/backend/src/__tests__/profiles.test.ts
@Dipti45sktech
Dipti45sktech requested a review from ShantKhatri May 13, 2026 17:20
@ShantKhatri
ShantKhatri merged commit e136375 into Dev-Card:main May 16, 2026
1 check failed
@Harxhit Harxhit added gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. level:intermediate Moderate difficulty contribution requiring some project understanding. (+35 pts) type:testing Adds/improves tests (+10 pts) labels May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. level:intermediate Moderate difficulty contribution requiring some project understanding. (+35 pts) type:testing Adds/improves tests (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backend: add vitest tests for profile GET /me and PUT /me routes

3 participants