Note: This project is currently in a work in progress so not all languages or sections are complete.

Test Driven Development with NodeJS

Installing Homebrew

We're going to start by installing Homebrew which will make installing everything else we need much easier.

Open a Terminal window and run the following commands:

1
2
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing NodeJS

Now we can easily install NodeJS with Homebrew using the following:

1
brew install nodejs

Creating the Project

Now we'll use npm to initialize a project for us.

1
2
3
# npm is installed along with NodeJS
mkdir odenay && cd odenay && npm init --yes
cd odenay

Dependency Management

When building software you will frequently use libraries and tools from a third party. Since they have their own development lifecycles and versions we want to be able to define a specific version of that dependency to use in our software. We want to do that with a tool and a file.

For NodeJS we'll use the tool npm and our dependencies will be defined in the file package.json.

1
# npm is installed along with NodeJS

Adding a Testing Library

We're going to use the testing library Jasmine to write our tests.

Edit the file package.json and add Jasmine as a testing dependency:

1

Then we retrieve Jasmine by running npm install.

Finally we initialize Jasmine by running .

The First Test

Create the file test/odenay_test.js with the following contents:

1
2
3
4
5
6
describe("translateWord", function () {
  it("should translate the word", function () {
    var result = piglatin.translateWord("apple");
    expect(result).toBe("appleyay");
  });
});

Then run the test with the command jasmine to see the results:

1
2
3
4
5
6
7
8
9
10
11
12
13
Started
F

Failures:
1) translateWorld should translate the word
  Message:
    ReferenceError: piglatin is not defined
  Stack:
    ReferenceError: piglatin is not defined
        at Object.<anonymous> (/Users/bkelly/temp/piglatin-nodejs/spec/piglatin_spec.js:3:18)

1 spec, 1 failure
Finished in 0.005 seconds

Comments