Dependency Injection
Learn about dependency injection in fp-ts reader
Code Editor
05-dependency-injection.exercise.ts
💻
Loading editor...
Preparing Monaco Editor with TypeScript support
Test Results
Requirements
describe('Reader dependency injection', () => {
const logs: string[] = []
const mockLogger: Logger = {
log: (msg) => logs.push(msg),
}
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
]
const mockUserService: UserService = {
getUser: (id) => users.find(u => u.id === id),
}
const deps: Dependencies = {
logger: mockLogger,
userService: mockUserService,
}
beforeEach(() => {
logs.length = 0
})
it('finds existing user and logs', () => {
const result = getUserById(1)(deps)
expect(result).toBe('Alice')
expect(logs).toContain('Found user: Alice')
})
it('handles missing user and logs', () => {
const result = getUserById(99)(deps)
expect(result).toBe('User not found')
expect(logs).toContain('User 99 not found')
})
it('works with different user service', () => {
const differentService: UserService = {
getUser: () => ({ id: 999, name: 'Test User' }),
}
const testDeps = { ...deps, userService: differentService }
const result = getUserById(1)(testDeps)
expect(result).toBe('Test User')
})
})🧪
Ready to Test?
Click "Run Tests" to see how your code performs
Quick Tips
•Read the TODO comments in the code
•Use Reset to restore original code
•Check Solution if stuck
Pro Tips
💡 Stuck? Here's what to try:
- • Read the comments in the code carefully
- • Run tests frequently to get feedback
- • Check the fp-ts documentation
- • Use the solution if you need help
🚀 Learning Approach:
- • Focus on understanding, not just solving
- • Experiment with different approaches
- • Think about real-world applications
- • Build on previous exercises