Node.js Testing Strategies

RobC

Rob Conery teaches Node JS

Welcome to this review of the Pluralsight course Node.js Testing Strategies by Rob Conery

Rob has been working in the technology field full time since 1998 as a DBA and then a web developer. His original focus was the Microsoft ASP.NET stack, building tools like Subsonic and the first Micro-ORM: Massive.

He co-founded the online training site Tekpub.com with James Avery and co-host This Developer’s Life with Scott Hanselman.

Tekpub was bought out by Pluralsight and he worked there for two years. He is currently working on a new book called The Imposters Handbook.

Also in this series:

Part 2 – Simple Tests with Node.js and Mocha
Part 3 – Data Access Considerations
Part 4 – External APIs

Introduction

In this course we’re getting in a rocket ship and flying to Mars!

145px-Mars_23_aug_2003_hubble

Planet Mars

We’re building a rocket shop signup system using Stripe, a much loved mobile payment solution. Rob has used Stripe for many years and you can find comprehensive documentation at https://stripe.com/docs

We’ll be using the SinonJS mocking framework and the prerequisites for this course are:

  • Familiarity with Node JS and the basics of how it works (see here if you don’t)
  • Testing (Unit testing and Behavior Driven Design)
  • Written software before (not a beginners course)
  • Willing to take your time

In this course you will learn:

  • The basic testing tools in Node
  • Basic Testing Strategies
  • Working with data and a database
  • Mocking, stubbing and asynchrony

Installation

Rob is a big fan of WebStorm and explains the reasons why he recommends it. He also recommends Atom.

However we’ll be using Sublime Text in this course.

The other software we’re using is:

  • Node JS
  • Mocha JS testing framework
  • Sinon.JS for spies
  • Moment.js for working with dates

Rob explains how to install these, how to run mocha, and discusses the power of the npm test command.

We npm init to create our package.json and see that NPM senses the mocha is likely to be the tool what want for testing.

Rob writes a quick BDD style mocha test as a demonstration, and we see the nicely formatted mocha output. This covers nearly all the essentials of BDD in just a couple of minutes.

Rob also shows how to create a git repository for our project.

Optional Installations

This takes a look at Chai, Should and CoffeeScript.

Rob also discusses the standard Node assert library.

Rob sometimes uses CoffeeScript for writing cleaner unit tests. That idea never came to me before, but I think it is a good one.

We see how to setup Mocha with CoffeeScript using a mocha.opts file and we see that the tests are much more readable without all of the brackets and other ugliness.

Looking at the same test in CoffeeScript and JavaScript side by side is quite striking.

Setting up Webstorm

Huh? I thought we we using Sublime for this course? If you don’t want to use WebStorm you can safely skip this lesson.

However this can help you get this IDE setup for Node JS development. We get syntax highlighting and italicization, and setup Mocha.

We see the test window that runs our tests and looks nicer than using the command prompt.

We also see the debugger in action.

A First Test

We’re back to use Sublime Text and using Ken Wheeler’s Brogrammer Theme.

First we create a new module for subscriptions using npm init. 

This first test is a happy path test, with no assertions, so this only serves as a guide to the initial structure of a test.

There may be times when we just want to write some early documentation for ourselves and assertion free tests achieve that. Here we’re describing our mars mission application.

We see that mocha categorizes this as a pending test.

Continue to Part 2 –  Simple Tests with Node.js and Mocha

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s