Test-driven development
with Node.js
@mirkokiefer
Mirko Kiefer - mail@mirkokiefer.com
What is TDD?
Why use TDD?
• Less fear of change
• Increase productivity and focus
• Better communication in teams
• Improved software design
• Less bugs
Test in layers
• Start with the public interface (API / user interface)
• Mock the next (non-implemented) layer underneath
• Repeat with implementing the next layer
• Example:

user interface -> server API -> request handlers -> database
Write failing test
• Imagine the interface you wish
• Does not have to compile
• Keep it simple
Make test pass
• Quickest solution possible
• Sins are allowed
• Fake it till you make it

Refactor
• Remove duplication
• If obvious, write “correct” implementation
• Write next failing test

Fake it till you make it!
How to fake it?
Dependency Injection
Enter proxyquire()
• Dependency injection without changing your code
• Does not fiddle with globals
Spies and stubs
self-made
Spies and stubs
Sinon.JS
Code coverage
Istanbul
Code coverage
• Don’t be fanatic
• Code covered does not mean its tested
• You know which code is critical
Keep it simple
• Do you really need tools for mocking and
dependency injection?
• Tools introduce complexity and can slow you down
• Embrace concepts, not tools
Mirko Kiefer
@mirkokiefer
mail@mirkokiefer.com
Questions?

Test-driven development with Node.js