Backcountry Sights

Photos and stories from the outdoors

-code With Mosh- Mastering Javascript Unit Testing May 2026

function applyDiscount(user, total) { if (user.type === 'VIP') return total * 0.8; return total; }

"Most developers think testing is about finding bugs," Mosh said, drawing a red circle around a piece of code. "That’s a lie. Testing is about . If your code is hard to test, it’s badly designed." -Code With Mosh- Mastering JavaScript Unit Testing

"That’s it," Sarah said, her voice eerily calm. "You’re not writing a single line of new code until you learn how to test the old code." function applyDiscount(user, total) { if (user

test('should use credit card if PayPal fails', async () => { // Mock the failing PayPal service const mockPayPal = jest.fn().mockRejectedValue(new Error('Timeout')); const result = await processPayment(mockPayPal, 'creditCard'); If your code is hard to test, it’s badly designed

She started laughing. "Best thirty dollars this company ever spent." Six months later, Leo wasn't a firefighter anymore. He was the team's testing evangelist. New hires came to him with shaky pull requests, and he'd say the same thing Mosh said to him:

Because Leo finally understood: writing tests wasn't about proving his code worked today. It was about having the courage to change it tomorrow.

test('apply 20% discount to VIP users', () => { const user = { type: 'VIP' }; const total = 100; const result = applyDiscount(user, total); expect(result).toBe(80); }); He ran it. The function didn't exist yet.

© 2025 Backcountry Sights

Theme by Anders Norén