And that made him a real engineer.
He still watched Code With Mosh videos on the train, moving on to Mastering TypeScript and Design Patterns . But he never forgot that first green checkmark.
Leo paused the video. He looked at his own checkout.js file—a 500-line monster with nested conditionals, global variables, and functions that did seven things at once. No wonder it broke.
Because Leo finally understood: writing tests wasn't about proving his code worked today. It was about having the courage to change it tomorrow. -Code With Mosh- Mastering JavaScript Unit Testing
"Watch this."
expect(result.method).toBe('creditCard'); });
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. And that made him a real engineer
"So," she said. "Did Mosh save you?"
Mosh drew a diagram. "Don't test the database. Test your logic. Replace the real dependency with a mock." Leo learned to write:
test('calculate total price for two items', () => { // Arrange const cart = [{ price: 10 }, { price: 20 }]; // Act const result = calculateTotal(cart); // Assert expect(result).toBe(30); }); Leo typed along. For the first time, he ran npm test and saw that beautiful green checkmark. Passed. Leo paused the video
He ran the tests again.
It felt… clean. The next lesson hit him like a truck. Mosh introduced Test-Driven Development (TDD) .
He felt a strange rush. It wasn't the dopamine hit of shipping messy code fast. It was the quiet confidence of building a brick wall, one perfect brick at a time. The hardest chapter was Mocks & Stubs . Leo had an API call to fetchUserPaymentMethod . In production, this called a slow database. In tests, it was a nightmare.
Leo had been a JavaScript developer for three years. He could spin up a React component in his sleep and chain promises like a poet. Yet, every Friday evening, the same dread washed over him as he typed npm run build .