Unit testing Ajax requests with Mocha

March 21, 2015 – 2:05 pm Tags: , ,

Ajax requests are some of the best things you can test. It’s very easy for them to go wrong (server issues? connection issues? etc.), so a bug in error handling can be very frustrating to users. They are also one of the boundaries of your code – places where data is loaded from the outside world – and those points are often prone to errors.

But testing them can be tricky. It’s asynchronous, and 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 the rest of this article

ESLint vs JSLint vs JSHint vs JSCS

March 5, 2015 – 11:14 pm Tags: ,

I recently wrote about using ESLint to auto-detect bugs. But what makes ESLint the best tool for this job?

There are three other popular JavaScript linters:

  • JSLint
  • JSHint
  • JSCS

What are their pros and cons? How do they compare against ESLint?

Read the rest of this article

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

March 1, 2015 – 7:12 pm Tags:

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 the rest of this entry »

Detect problems in JavaScript automatically with ESLint

February 10, 2015 – 1:06 pm Tags: , ,

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?

January 30, 2015 – 5:16 pm Tags: ,

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 the rest of this entry »

Strange JavaScript errors and how to fix them

January 22, 2015 – 9:30 pm Tags:

JavaScript has some of the strangest errors. Undefined is not a function? Ugh.

I wrote an article talking about JS errors and a guide on how to fix them on David Walsh’s blog, you should go check it out!

JavaScript errors and how to fix them

ES6: What are the benefits of the new features in practice?

January 6, 2015 – 11:00 am Tags:

You’ve probably seen people talk about the features in the upcoming JavaScript version, ES6, or ECMAScript 6. Promises, let keyword, iterators, etc.

But why should you care? I certainly heard a lot about it, but I hadn’t looked into it because I wasn’t interested in theory, but rather the benefits I would get in my day to day work. Is it just feature-bloat for sake of having fancy things in the language, or are they actually useful?

I then did some research and discovered a number of very useful features in ES6 that you can start taking advantage of right now. Let’s take a look at the most useful ones

Read the rest of this entry »

How to start using ES6 today?

December 18, 2014 – 3:36 pm Tags:

When you want to use ES6 you’re faced with a list of questions: Transpilers, shims, browser support…

In this article, I’ll give you a detailed guide on making sure you get started with ES6 the right way, and instructions on how you can set up a workflow for using ES6 in production easily.

Read the rest of this entry »

How to make your code self-documenting?

December 1, 2014 – 5:55 pm

Isn’t it fun to find a comment in code that’s completely out of place and useless? What if you could write fewer comments and still keep the code easy to understand?

One of the primary ways to do this is making your code-self documenting. When code is self-documenting, it doesn’t need comments to explain what it does or its purpose, which is great for making the code easier to maintain. As a bonus, with fewer comments, it’s less likely they’ll be crap!

In this article, I will show you several ways you can make your code document itself.

Read the rest of this entry »

AngularJS best practices: Be careful when using ng-repeat’s $index

November 10, 2014 – 12:54 pm Tags: ,

“A customer reported they deleted an item and the wrong item got deleted!”

Sounds like a pretty serious bug. This is what we got one time at work. Attempting to locate it was quite difficult, because naturally the customer had no idea what they did to reproduce the issue.

Turns out the bug was caused by using $index in an ng-repeat. Let’s take a look at how this happens, and a super simple way to avoid this type of bug entirely, and also a few lessons we can learn from this.

Read the rest of this entry »