Test: add vitest tests for GET and PUT /api/profiles/me routes#76
Test: add vitest tests for GET and PUT /api/profiles/me routes#76Dipti45sktech wants to merge 2 commits into
Conversation
|
Hi @ShantKhatri, would really appreciate if you could review my PR whenever available. |
| // }); | ||
| // if (existing) { | ||
| // return reply.status(409).send({ error: 'Username already taken' }); | ||
| // } |
There was a problem hiding this comment.
The commit is removing the existing changes, and not change added, no test is added.
There was a problem hiding this comment.
Really sorry about that @ShantKhatri ! When I was setting up the test file, I deleted the old placeholder file and created the new one, but didn't verify it was actually being tracked by git before committing. So the commit ended up only showing the deletion and not the new file. I caught the mistake, recreated the file with all the tests, verified all 5 pass locally, and pushed the updated commit to the same branch. The test file should now be properly visible in the PR. Really sorry for the confusion, please have a look now!
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
What Changed
apps/backend/src/__tests__/profiles.test.tswith full test coverageapp.prisma.user.findUnique,findFirst, andupdateusingvi.fn()buildApp()helper that spins up a test Fastify instance with mocked auth and prismaHow to Test
pnpm installpnpm --filter @devcard/backend testChecklist
pnpm -r run lintpasses).pnpm -r run typecheck).pnpm -r run test).console.logor debug statements left in the code.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.