Preparing for Test Driven Development with Intern.js and BrowserStack

"Experience is a hard teacher because she gives the test first, the lesson afterwards."
~Vernon Saunders Law

Lesson Goal

This lesson will setup a new application using Intern.js and BrowserStack for TDD.

Why do TDD?

You should use TDD for many reasons, including:

"If it's worth building, it's worth testing. If it's not worth testing, why are you wasting your time working on it?"
~Scott W. Ambler

Assumptions

Start a basic app

mkdir chatapp
cd chatapp
npm init

Provide your initial npm package.json details

name: (chatapp)
version: 0.0.1
description: an app for rapid-fire chatting
entry point: (index.js) 
test command: ./node_modules/.bin/intern-client config=tests/intern
git repository: YourGitHubName/chat-app
keywords: demo, chat, intern.js, gun
author: YourName
license: (Zlib OR MIT OR Apache-2.0)
About to write to /Volumes/.../Users/.../projects/chatapp/package.json:
...
Is this ok? (yes) 

Install Intern

sudo npm install intern --save-dev

You will be prompted for your password.

Set up file structure

mkdir tests  // for tests
mkdir app    // for deployment code

Copy then open the intern config file intern.js

cp node_modules/intern/tests/example.intern.js tests/intern.js
// Open test/intern.js in a text editor such as Sublime or Textmate, e.g.
subl tests/intern.js

Point config to app directory

// edit 
packages: [ { name: 'myPackage', location: '.' } ]
// to
packages: [ { name: 'ChatApp', location: 'app' } ]

Verify initial test setup

./node_modules/.bin/intern-client config=tests/intern

// verify the test command response is
0/0 tests failed

The intern-client command runs unit tests locally.

Setup local functional testing

Modify the tunnel config option

Modify the environment config option

Start the chromedriver

From the command line run

chromedriver --port=4444 --url-base=wd/hub

Verify the local functional test set-up

This command uses intern-runner instead of intern-client.

Review test results

Listening on 0.0.0.0:9000
Starting tunnel...
Initialised chrome 43.0.2357.132 on Mac OS X
No unit test coverage for chrome 43.0.2357.132 on Mac OS X
chrome 43.0.2357.132 on Mac OS X: 0/0 tests failed

TOTAL: tested 1 platforms, 0/0 tests failed

Set-up BrowserStack

Verify the remote functional test set-up

Add additional browsers

At this point if you get a TypeError: undefined is not a function logged to the console, do not worry.

Prepare to Test Locally

Test Locally

Create first functional test file

Create first functional test

Hello World: Red

Hello World - Local

Hello World - BrowserStack

Listening on 0.0.0.0:9000
Starting tunnel...
BrowserStackLocal v3.7
Ready
Initialised chrome 34.0.1847.116 on MAC
No unit test coverage for chrome 34.0.1847.116 on MAC
chrome 34.0.1847.116 on MAC: 0/1 tests failed
Initialised firefox 39.0.3 on MAC
No unit test coverage for firefox 39.0.3 on MAC
firefox 39.0.3 on MAC: 0/1 tests failed
Initialised internet explorer 11 on WINDOWS
No unit test coverage for internet explorer 11 on WINDOWS
internet explorer 11 on WINDOWS: 0/1 tests failed

TOTAL: tested 3 platforms, 0/3 tests failed

Conclusion

"Only a fool tests the depth of the water with both feet."
~ African proverb