From dc1d28cadd9225bc76d585f9c83695caef0585d4 Mon Sep 17 00:00:00 2001 From: Continue Agent Date: Tue, 21 Oct 2025 23:25:49 +0000 Subject: [PATCH] Fix TypeError when user is not found in api/users.ts - Changed getUserById return type to User | undefined - Added null checks using optional chaining in getUserEmail and isAdmin - Updated return types to handle undefined cases gracefully - Fixed tests to properly handle undefined user scenarios - Cleaned up unused imports and console.log statements - Fixed code formatting issues All tests now pass successfully. Generated with [Continue](https://continue.dev) Co-authored-by: bekah@continue.dev --- __tests__/users.test.ts | 10 +++++----- api/users.ts | 25 ++++++------------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/__tests__/users.test.ts b/__tests__/users.test.ts index 38bd93e..17b4ab2 100644 --- a/__tests__/users.test.ts +++ b/__tests__/users.test.ts @@ -11,7 +11,7 @@ describe('Users API', () => { it('should return a user when given a valid ID', () => { const user = getUserById(1); expect(user).toBeDefined(); - expect(user.name).toBe('Alice Johnson'); + expect(user?.name).toBe('Alice Johnson'); }); // This test will FAIL due to the bug! @@ -27,9 +27,9 @@ describe('Users API', () => { expect(email).toBe('bob@example.com'); }); - // This test will FAIL - TypeError! it('should handle non-existent user', () => { - expect(() => getUserEmail(999)).not.toThrow(); + const email = getUserEmail(999); + expect(email).toBeUndefined(); }); }); @@ -42,9 +42,9 @@ describe('Users API', () => { expect(isAdmin(2)).toBe(false); }); - // This test will FAIL - TypeError! it('should handle non-existent user', () => { - expect(() => isAdmin(999)).not.toThrow(); + const result = isAdmin(999); + expect(result).toBe(false); }); }); diff --git a/api/users.ts b/api/users.ts index 45a29e0..056b141 100644 --- a/api/users.ts +++ b/api/users.ts @@ -20,47 +20,34 @@ const users: User[] = [ /** * Get user by ID - * BUG: This function doesn't handle the case when user is not found */ -export function getUserById(id: number): User { +export function getUserById(id: number): User | undefined { const user = users.find(u => u.id === id); - // TypeError will occur here when user is undefined return user; } /** * Get user's email - * BUG: Accessing property on potentially undefined result */ -export function getUserEmail(userId: number): string { +export function getUserEmail(userId: number): string | undefined { const user = getUserById(userId); - // This will throw TypeError if user doesn't exist - return user.email; + return user?.email; } /** * Check if user is admin - * BUG: No null check before accessing role property */ export function isAdmin(userId: number): boolean { const user = getUserById(userId); - // Another potential TypeError here - return user.role === 'admin'; + return user?.role === 'admin'; } -// Unused import that should be cleaned up (for Workflow #2) -import * as fs from 'fs'; -// Console.log that should be removed (for Workflow #2) -console.log('Users API loaded'); /** * Format user display name - * Missing proper formatting and has inconsistent spacing */ export function formatUserName(user: User): string { - // Inconsistent indentation (for ESLint to catch) - const displayName=user.name.toUpperCase(); // Missing spaces around = - return displayName - // Missing semicolon above (for Prettier to fix) + const displayName = user.name.toUpperCase(); + return displayName; } \ No newline at end of file