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, …
JavaScript Testing Tool Showdown: Sinon.js vs testdouble.js
When unit testing real-world code, there are many situations that make tests hard to write. How do you check if a function was called? How do you test an Ajax call? Or code using setTimeout? That’s when you use test doubles — replacement code that makes hard to test things easy to test. For many years, Sinon.js has been the …
5 step method to make test-driven development and unit testing easy
What’s the hardest thing in test-driven development or unit testing in general? Writing tests! The syntax or the tools aren’t the problem – you can learn those enough to get started in 15 minutes. The problem is how to take some vague idea in your head about what you want to do, and turn it into something that verifies some …
What is property based testing (and how to do it in JavaScript)?
Ever wonder… what if you didn’t have to write specific tests with hardcoded test data? Or have you ever written a function which can take many different values as input? How can you test something like that easily with different values? These are the kinds of important questions in life that keep me up at night! (ok maybe not) But …
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 …
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 …
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 …
What are Unit Testing, Integration Testing and Functional Testing?
Finding your way around the maze that is JavaScript testing can sometimes be difficult. There are unit tests, integration tests, functional tests, E2E tests, browser tests… With so many buzzwords, who knows what they do and which one to use, what for, and when? To help with that problem, in this article I’ll give you a guide comparing the different …
Unit testing Ajax requests with Mocha
Ajax requests can easily go wrong. You can’t guarantee the connection and the server always work correctly. They are also often used to send user input to the server and back, so it’s vital the data is handled correctly. But testing them can be tricky. It’s asynchronous, and also a good unit test must be isolated, so how can we …