How do I do TDD (Test Driven Development) with Node? And how do I do it in Intellij.
(For reference I am using Intellij 14.1)
Be warned this is a brain dump from me as I poke around and
figure out node, there is going to be mistakes made by me along the way…
Which Test Framework to use?
Before I start down this path it’s a good idea to do some
research.
Here are a few of the websites and videos I watched to help
me make my decision.
Testing with mocha
Test-driven
Development of Web Apps in Node.js
Node.js Development
Workflow in WebStorm
https://youtu.be/xuXIBSa_7j4 [3]
Node.js Modules
Tutorial #8 - Testing
Building Applications
with Node JS Course over 4 hours by JumpStart
So which one?
I am going to use Mocha for the test framework http://mochajs.org/ [6] and Chai for the assertion library http://chaijs.com/ [7]
Create a new Node Project
I am going to start fresh, create a new Node project in
Intellij 14.1
I already have the NodeJS plugin installed for Intellij
14.1, if you don't have that set up check out my write up on how to do that
at http://www.whiteboardcoder.com/2015/04/intellij-and-nod.html
From Intellij Create a new Project
From the menu select File -> New -> Project
Select Node.js and NPM and click Next.
Give it a name and click Finish.
This pops up. (for me).
This is supposed to configure the Intellij so that it can
have Code Completion on the Base NodeJS libraries. I can't seem to set it up right. So I click cancel (and set it up manually).
To set up code completion for Node base libraries do the
following.
From the File menu select Settings
Under Languages & Frameworks -> JavaScript select
Libraries
Checkbox nod-DefinitelyTyped and click OK.
(If you don't have this installed you will need to click
download, Select TypeScript Community
stubs from the pull down and download and install node)
Setting up tests
Install mocha and chai via the npm command line tool.
Click terminal in the lower left of Intellij
Run the following commands
> npm install -g
mocha
> npm install chai
|
Create a test folder
Right click on the project and select New -> Directory.
Name it test
Create a my-app.test.js file in the tests folder.
Add a test
Put this code in to test the test.
var assert = require('chai').assert;
describe('My App', function() { describe('Testing equality', function() { it('1 should equal 1', function () { assert.equal(1, 1); }); }); }); |
From the command line run the tests
> mocha
test/my-app.test.js
|
As a another test I created a second test file. numbers.test.js
Put the following code in it.
var assert = require('chai').assert;
describe('Number Testing', function() { var data = {'names':['joe', 'lisa', 'fred']} describe('Testing equality', function() { it('2 should equal 2', function () { assert.equal(2, 2); }); }); describe('Testing array Size', function() { it('Array should contain 3 names', function () { assert.equal(data.names.length, 3); }); }); }); |
From the command line run the tests
> mocha test/numbers.test.js
|
Mocha is smarter
You don't have to designate the file name you can just give
it a folder name and it will run all the tests it finds in the given folder.
From the command line run this
> mocha test
|
It ran all the test from both files.
As a quick test I created a subfolder and put a test in it.
Then ran
> mocha test
|
It did not see the subdirectory…. That is a problem.
I found this post on the subject http://bpinto.github.io/posts/running-mocha-tests-on-subdirectories/
[8] which shows how, you need to use the -recursive flag
> mocha --recursive test
|
Now it runs all the tests.
Add test script to package.json
You can edit the package.json file to add a script to run
the tests.
Here is how I edited my file.
{
"name": "Node_TDD_Test", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www", "test": "mocha -recursive test" }, "dependencies": { "body-parser": "~1.12.0", "cookie-parser": "~1.3.4", "debug": "~2.1.1", "express": "~4.12.2", "jade": "~1.9.2", "morgan": "~1.5.1", "serve-favicon": "~2.2.0" } } |
Now I can run this from the command line to run the script.
> npm test
|
Testing on Intellij
OK, now that I have testing working what is the best way to
integrate it with Intellij?
Webstorm seems to have a simple way to set this up https://www.jetbrains.com/webstorm/help/running-nodeunit-tests.html
[9]. I don't have Webstorm, but I may need to
invest in it sooner rather than later.
Looks like it's only $49 for an individual license.
Well if you don't want to pay for Webstorm, yet, but have
Intellij like I do what is the best thing to do?
Click on Edit Configurations on the top.
Click +
Select Mocha
Click here and locate the test directory
Change its name
Change the User Interface to tdd
Go locate the Mocha Package.
Mine was located at
C:\Users\patman\AppData\Roaming\npm\node_modules\mocha
Click OK.
Run the tests
That ran, but it did not get my subdirectory tests.
Add --recursive to the mocha options. And run it again.
Much better.
References
Accessed 4/2015
[2] Test-driven
Development of Web Apps in Node.js
Accessed 4/2015
[3] Node.js
Development Workflow in WebStorm
Accessed 4/2015
[4] Node.js
Modules Tutorial #8 - Testing
Accessed 4/2015
[5] Building Applications with
Node JS Course over 4 hours by JumpStart
Accessed 4/2015
[6] MochaJS home Page
Accessed 4/2015
[7] Chai Assertion Library
Accessed 4/2015
[8] Running mocha tests on subdirectories
Accessed 4/2015
[9] Running Nodeunit Tests
Accessed 4/2015
Thanks a lot for the tutorial.
ReplyDeleteFor me it had to be "test": "mocha --recursive tests" (with double -) in the package.json. Just a little think. :)
You help me a lot, thank you.
Thanks for the info. Also glad to hear this article was helpful to you :)
Delete