-code With Mosh- Mastering Javascript Unit Testing May 2026
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.
"Thirty bucks."
"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
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:
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 . test('calculate total price for two items', () =>
Leo decided to rewrite the cursed discount function. He opened a new file: discount.test.js .
He wrote the simplest possible code to turn it green: "Thirty bucks
Sarah blinked. "How much did that course cost?"