One of the biggest stumbling blocks when writing unit tests is what to do when you have code that’s non-trivial.
In real life projects, code often does all kinds of things that make testing hard. Ajax requests, timers, dates, accessing other browser features… or if you’re using Node.js, databases are always fun, and so is network or file access.
All of these are hard to test because you can’t control them in code. If you’re using Ajax, you need a server to respond to the request, so as to make your tests pass. If you use setTimeout, your test will have to wait. With databases or networking, it’s the same thing — you need a database with the correct data, or a network server.
Real-life isn’t as easy as many testing tutorials make it look. But did you know there is a solution?
By using Sinon, we can make testing non-trivial code trivial!