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 spies, stubs and mocks?
  • When and how to use each of them for best results
  • Common pitfalls and how to avoid problems

Read the rest of this article

AngularJS: Setting up parallel / sticky states with ui-router and ui-router-extras


The application I’m developing at work has many independent parts, which needed their own states. Think of a UI similar to Photoshop, WebStorm, or such – Every panel in the UI is self-contained and can be changed without affecting the others.

The standard ui-router has no concept of parallel states. Everything must be modeled as a tree, which means a setup like this doesn’t work. For example, changing one panel’s state would affect everything else too.

Thankfully there’s ui-router-extras, which adds support for so-called “sticky states” or “parallel states”. We can use this to have as many individual parts, that have their own parallel state trees, as we want.

Let’s go through a small sample app and look how to set this up step by step.

Read the rest of this entry

Big thanks to ui-router-extras author Chris Thielen for helping with this.

How to fix JavaScript errors more easily with Chrome’s debugger


Let’s face it: As developers, much of our time is spent fixing problems. No matter how hard I try, there’s always something that needs debugging. In the past, I would use console.log or alert. Just sprinkle them in my code, and hope that they give me enough information to fix the problem.

If you’ve tried doing that, you know that it’s not a very good approach. It’s like giving a surgeon a machete instead of a scalpel – not accurate and a bad tool for the job.

But just like surgeons have scalpels, we have better tools available for us too: Namely, debuggers. In this article, I’ll show you how to make use of Chrome’s JavaScript debugger to help you fix errors faster and more easily.

Read More

How to deal with promises in JavaScript unit tests?


One of my readers, Richard, sent me an email asking about promises in tests. It’s difficult to figure out how to put together a complete testing setup for code that uses promises, as there are so many different alternatives and so little information.

I wanted to help, so in this article, I’ll tell you exactly what to do when you need to use promises in unit tests:

  • How to integrate promises in unit tests
  • Which libraries can help?
  • 8 common promise-related testing scenarios
  • An example project showing how to put all of this together in practice

Head on over to the link to read more:

Promises in JavaScript Unit Tests: the Definitive Guide

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 kinds of testing types available, and some recommendations for their use.

Read More

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 do that when the code talks to the server?

Let’s take a look at some examples to learn how to test Ajax requests with ease.

Read More

What’s the difference between Unit Testing, TDD and BDD?


When you’re just getting started with automating your JavaScript testing, there’s a lot of questions. You’ll probably see people talk about unit testing, TDD or Test-Driven Development, and BDD or Behavior-Driven Development. But which one of them is the best approach? Can you use all of them?

I’ve talked to a number of JavaScript developers, and there seems to be some confusion about all this. So, let’s take a look at Unit testing, TDD and BDD, and fix some of the common misconceptions about them out there.

Read More

Detect problems in JavaScript automatically with ESLint


I’ve been programming JavaScript for 15 years or so, and I still keep making various silly mistakes – I type things wrong, I forget to rename everything, I forget to follow the coding styles… then I waste my time clicking around in the browser and feel like slapping myself.

Thankfully tools like ESLint exist. ESLint helps by finding errors automatically, so you can spend your time doing something more interesting.

Let me show you a real-world example from a codebase I worked on of how ESLint can help.

Read the rest of this article

How to unit test NodeJS HTTP requests?


I have a nodejs app where I wanted to unit test some HTTP requests. Node usually makes things simple, so I expected this to be like that too… but I soon discovered that this was not the case at all.

When unit testing, you don’t want HTTP requests to go out and affect the result. Instead you create a fake request – a test double – to replace the real one, but because node’s streams are hard to fake, this is where I hit the wall.

It took some work, but I found a way to make it simple! Let me show you how with some examples based on real live production code.

  1. Sending a GET request and testing response handling
  2. Sending a POST request and testing the parameter behavior
  3. Testing that failures are handled correctly

Read More