Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions __tests__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -27,9 +27,9 @@ describe('Users API', () => {
expect(email).toBe('[email protected]');
});

// This test will FAIL - TypeError!
it('should handle non-existent user', () => {
expect(() => getUserEmail(999)).not.toThrow();
const email = getUserEmail(999);
expect(email).toBeUndefined();
});
});

Expand All @@ -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);
});
});

Expand Down
25 changes: 6 additions & 19 deletions api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading