An introduction on testing Node.js code.
Covers frontend (UI) testing, backend unit tests and code coverage.
Also mentions how to use a Makefile to run frontend and backend tests at the same time.
5. What is Node.js?
example.js
var
http
=
require('http');
http.createServer(function
(request,
response)
{
response.writeHead(200,
{'Content-‐Type':
'text/plain'});
response.end('Hello
Worldn');
}).listen(8080);
console.log('Server
running
at
http://127.0.0.1:8080/');
Running it
$
node
example.js
Server
running
at
http://127.0.0.1:8080/
5
6. Node is non-blocking
Blocking code
var
fileContents1
=
fs.readFileSync('file1.txt');
console.log(fileContents1);
var
fileContents2
=
fs.readFileSync('file2.txt');
console.log(fileContents2);
0s 5s 10s
6
23. UI testing
SpookyJS is a SpookyJS
scriptable web testing
framework for Mocha CasperJS
Wrapper for CasperJs
and PhantomJS
Uses WebKit, so
WEBKIT
supports client-side
Javascript
23
24. var
Spooky
=
require('spooky');
var
spooky
=
new
Spooky(
{
SAMPLE SPOOKY SCRIPT
child:
{
port:
8080,
script:
'./lib/bootstrap.js',
//Loads
casperJS
spooky_lib:
'./node_modules'
}
},
function
(err,
error,
response)
{
if
(err
||
error)
{
var
e
=
new
Error('Failed
to
initialize
SpookyJS');
e.details
=
err
||
error;
throw
e;
}
spooky.on('error',
function
(e)
{console.error(e);});
spooky.on('console',
function
(line)
{console.log(line);});
spooky.start();
spooky.then(function
(){
this.echo('Hello,
this
is
SpookyJS');
});
spooky.open('http://www.google.com/');
spooky.then(function
()
{
this.echo('Now
viewing:
'
+
this.getCurrentUrl());
});
spooky.run();
}
);
24
25. var
util
=
require('util');
var
expect
=
require('expect.js');
describe("Test
that
SpookyJS
is
working",
function
()
{
var
context
=
{};
TEST/FRONTEND.JS
var
hooks
=
require('../util/hooks');
before(hooks.before(context));
describe('Test
that
SpookyJS
can
navigate
to
Google',
function
()
{
it('navigates
to
google.com,
and
returns
the
current
url',
function
(done)
{
context.spooky.start();
context.spooky.then(function
(){
this.echo('Hello,
this
is
SpookyJS');
});
context.spooky.open('http://www.google.com/');
context.spooky.then(function
()
{
this.echo(this.getCurrentUrl());
});
function
onConsole(line)
{
if
(line
===
'http://www.google.com/')
{
context.spooky.removeListener('console',
onConsole);
done();
return;
}
}
context.spooky.on('console',
onConsole);
context.spooky.run();
});
});
after(hooks.after(context));
});
25
32. Testing the frontend
Makefile
test:
@./node_modules/.bin/mocha
$$(find
test/frontend
-‐name
"*.js")
-‐r
expect.js
-‐R
spec
Run tests
$
make
testfrontend
32
33. Makefile
testbackend:
@./node_modules/.bin/mocha
$$(find
test/backend
-‐name
"*.js")
-‐r
expect.js
-‐R
spec
testfrontend:
@./node_modules/.bin/mocha
$$(find
test/frontend
-‐name
"*.js")
-‐r
expect.js
-‐R
spec
coverage:
@echo
'Checking
test
code
coverage...'
...
@echo
'Done.
Result
written
to
coverage.html.'
test:
@./node_modules/.bin/mocha
$$(find
test/
-‐name
"*.js")
-‐r
expect.js
-‐R
spec
all:
test
coverage
@echo
'Tested
frontend
and
backend.
Coverage
doc
saved
to
coverage.html.'
Run all tests
$
make
test
Run all tests + show test coverage
$
make
all
33
34. Summary
Node.js is Javascript on the server.
Unit testing with Mocha + Expect.js
Checking coverage with JSCoverage
User interface testing with SpookyJS
Running everything with Make
34
37. References
Node.js
http://nodejs.org/
Unit testing (Mocha)
http://visionmedia.github.com/mocha/
Test coverage
http://tjholowaychuk.com/post/18175682663/mocha-test-coverage
User interface testing
http://casperjs.org/
https://github.com/WaterfallEngineering/SpookyJS
Other useful stuff
https://npmjs.org/ - Learn about commonly used node packages
http://jenkins-ci.org/ - Set up continuous integration (e.g. Automatically testing when you commit)
37