Something that’s not talked very often when it comes to writing unit tests is how should you structure your test code? I’m not talking about “well, just put your code into a file in test/SomeTest.js”, but rather, how should the test code itself within your test case be structured? While it may feel inconsequential – afterall, it’s just a test, …
How many tests is too many?
Some time ago I stumbled upon some crazy stuff… Specifically, I found out that SQLite has 787 times more tests than they have actual code! It’s no joke, it’s documented right on their website. While they have about 116 300 lines of source code, they have 91 577 300 lines of test code. That sounds completely insane. I bet you’ve …
Mongoose models and unit tests: The definitive guide
A korean translation of this article Mongoose is a great tool, as it helps you build Node apps which use MongoDB more easily. Instead of having to sprinkle the model-related logic all over the place, it’s easy to define the functionality as Mongoose models. Querying MongoDB for data also becomes quick and easy – and if you ever need some …
How to unit test ES6 code?
ES6 is here today. Several browsers have almost all ES6 features available, and even Microsoft Edge is getting there. Tools like Babel help cover the parts which browsers don’t have yet. If you want to start using ES6 for development, that’s easy: there’s a lot of detailed information available. But what about testing? How do you write unit tests for …
Sinon.js quick tip: How to stub/mock complex objects, such as DOM objects
Several of my readers have emailed me, asking about how to deal with more complex stubbing situations when using Sinon.js. In this article, we’ll look at how to stub objects which are deeply nested, and when functions have more complex return values and they interact with other objects. We’ll use DOM objects as a practical example, as they’re used quite …
What is a Humble Object and how does it help make your code testable?
Some of the most common testing-related questions I’m asked relate to testing real-world apps. There’s always a difficult part that’s hard to test. Most often, it’s been the database. Sometimes I’ve also been asked about HTTP-based APIs like RESTful APIs and such. Let’s imagine a typical situation where you have some code that uses a database. You’ve got some code …
Using Sinon.js to make unit testing real-life apps easy
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 …
Learn to unit test your javaScript code with Mocha and Chai
Have you ever made some changes to your code, and later found it caused something else to break? I’m sure most of us have. This is almost inevitable, especially when you have a larger amount of code. One thing depends on another, and then changing it breaks something else as a result. But what if that didn’t happen? What if …
Why is fixing bugs so slow? (and how to make it faster)
Have you ever wondered why sometimes fixing bugs seems to take much longer than it should? When you finally find the problem, it turns out all you need is one small change. Yet, it took a lot of time to find what’s going on. This happens to me more often than I’d like. On the other hand, when you’re writing …
Sinon.js best practices for spies, stubs and mocks
Sinon is one of the most important tools for testing, as without it writing tests for more complex pieces of code such as Ajax, networking, databases, etc. would become difficult. In this article, I’ll show you the best practices for using Sinon, so you’ll be able to apply it to your own projects more easily. What are the differences between …