Skip to content

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

Open
Dipti45sktech wants to merge 2 commits into
Dev-Card:mainfrom
Dipti45sktech:profile
Open

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

Conversation

@Dipti45sktech
Copy link
Copy Markdown

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
Author

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

// });
// if (existing) {
// return reply.status(409).send({ error: 'Username already taken' });
// }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The commit is removing the existing changes, and not change added, no test is added.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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!

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.

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

2 participants