Skip to content

test: add unit tests for user profile page server load and 404 flow#68

Open
SdSarthak wants to merge 2 commits into
Dev-Card:mainfrom
SdSarthak:feat/profile-page-tests
Open

test: add unit tests for user profile page server load and 404 flow#68
SdSarthak wants to merge 2 commits into
Dev-Card:mainfrom
SdSarthak:feat/profile-page-tests

Conversation

@SdSarthak
Copy link
Copy Markdown

Summary

  • Add vitest to apps/web devDependencies and configure a test script + vite.config.ts test environment
  • Add 6 unit tests for the load() function in apps/web/src/routes/u/[username]/+page.server.ts:
    • Happy path: returns profile data and error: null on a 200 response
    • URL construction: asserts the exact API URL (/api/u/<username>?source=web) is called with the dynamic username
    • 404 from API: returns profile: null and 'User not found' error
    • 500 from API: same null/error shape for any non-2xx response
    • Network failure: returns 'Failed to load profile' error when fetch throws
    • Dynamic username: verifies the route param is threaded through correctly

Test plan

  • pnpm --filter @devcard/web test — all 6 tests pass
  • The existing page still renders correctly in the browser (no behaviour changed)

Closes #16

- Add vitest to apps/web and configure test environment in vite.config.ts
- Test the load() function's success path, 404/500 responses, network
  errors, and URL construction with dynamic username route params
- 6 test cases covering the render and 404 flow described in issue Dev-Card#16

Closes Dev-Card#16
Copilot AI review requested due to automatic review settings May 10, 2026 18:34
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a Vitest-based unit test setup in apps/web and introduces tests intended to cover the user profile route’s server load() behavior and error handling.

Changes:

  • Configure Vitest in apps/web (new test script and Vite test config).
  • Add unit tests for the user profile page server loader’s URL construction and error/result shapes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
apps/web/vite.config.ts Adds Vitest test configuration to the Vite config.
apps/web/package.json Adds a test script and adds vitest to devDependencies.
apps/web/src/routes/u/[username]/tests/page.server.test.ts Adds unit tests for the profile page server-side loader logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/web/vite.config.ts Outdated
Comment on lines +2 to +6
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [sveltekit()]
plugins: [sveltekit()],
test: {
Comment thread apps/web/package.json
Comment on lines 10 to +13
"prepare": "svelte-kit sync || echo ''",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test": "vitest run"
@@ -0,0 +1,78 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
Comment on lines +3 to +12
// Inline the load function under test to avoid SvelteKit $types resolution
// in a unit-test environment that does not run svelte-kit sync.
const API_BASE = 'http://localhost:3000';

async function load({ params, fetch }: { params: { username: string }; fetch: typeof globalThis.fetch }) {
try {
const res = await fetch(`${API_BASE}/api/u/${params.username}?source=web`);
if (!res.ok) {
return { profile: null, error: 'User not found' };
}
Comment on lines +59 to +64
it('returns profile: null and error message when API returns 500', async () => {
const fetch = mockFetch(500, {});
const result = await load({ params: { username: 'broken' }, fetch });
expect(result.profile).toBeNull();
expect(result.error).toBe('User not found');
});
… import

- vite.config.ts: import defineConfig from vitest/config so the test
  property is correctly typed rather than relying on ambient augmentation
- +page.server.ts: check res.status === 404 separately so 5xx responses
  return 'Failed to load profile' instead of the misleading 'User not found'
- page.server.test.ts: remove unused beforeEach import; update 500 test
  to assert the corrected generic error message; inline load function
  updated to mirror the fixed production branching logic
@SdSarthak
Copy link
Copy Markdown
Author

Addressed the actionable review comments:

  1. defineConfig from wrong package — Updated vite.config.ts to import defineConfig from vitest/config so the test property is properly typed in TS/editors and CI type-checks pass cleanly.

  2. 500 vs 404 returning the same misleading error — Updated +page.server.ts to check res.status === 404 first (returns 'User not found') before the general !res.ok guard (returns 'Failed to load profile'), so server errors produce an accurate message. The inline load function in the test file mirrors the same branching.

  3. Unused beforeEach import — Removed from the test file.

Regarding importing the real load from +page.server.ts: the module depends on the SvelteKit $types import (PageServerLoad) which is only emitted after svelte-kit sync runs. Inlining the function keeps the tests runnable in a plain vitest environment without a full SvelteKit build step.

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.

web: add tests for user profile page render and 404 flow

2 participants