Unit Tests
Testing Tools
[Mocha or Jasmine]: The JavaScript test framework used to test our application code. See Mocha example on how to Run Only a Suite or Test Case.
Karma: It is a JavaScript test-runner built with Node.js to execute our application tests.
Sinon.JS API: A library used for mocking, stubbing, and spying within our unit tests. Standalone test spies, stubs and mocks for JavaScript. No dependencies works with any unit testing framework.
Chai Assert API: An assertion library used within unit tests.
PhantomJS is a headless WebKit scriptable with a JavaScript API. It is suitable for general command-line based testing.
Basic steps
- The unit: a piece of code MyClass.js
- The unit test: each unit test is a piece of code that validates the unit. Although it will seem tedious to keep every test isolated—which means you might have dozens of unit tests for one code unit!—it saves you time in the end. If you attempt to combine a bunch of cases into a single unit test, and that test fails, you’ll not immediately know which specific case failed. You’d then have to step through every case of that test in the debugger, and, finding that process to be too tedious and time-consuming, you’ll end up breaking out every case into its own individual unit test anyway.
- The test runner: we’ll run tests on the command line using Karma, which you’ll need to install along with Jasmine as follows:
Command | Purpose |
---|---|
npm install mocha --save-dev | [Old] Installs mocha |
npm install jasmine --save-dev | [New] Installs jasmine |
npm install --save-dev karma | Installs Karma |
npm install --save-dev karma-cli | Installs Karma cmd line |
npm install --save-dev karma-jasmine | Installs Karma dependencies: either mocha or jasmine |
npm install --save-dev karma-phantomjs-launcher | [Old] Installs Karma dependencies: phantomjs |
npm install --save-dev karma-chrome-launcher | [New] Installs Karma dependencies: chrome |
npm install --save-dev karma- |
replace |
Now you can run the tests from the command line:
karma start --single-run
or
karma init karma2.conf.js
karma start karma2.conf.js
or
karma init
>>> For location of source file: spec/*.spec.js
>>> For location of test file: src/*.js
code coverage
Karma can generate code coverage using awesome Istanbul. If you want to generate the coverage, you need to configure up to three parts: 1.reporter coverage (required) 2.preprocessor coverage (required) 3.reporter options (optional)
npm install karma-coverage --save-dev
>>> In Karma.conf.js Add ‘coverage’ to reporters
>>> In Karma.conf.js Add 'src/*.js': ['coverage'] to preprocessors
You should not however include the files that aren't directly related to your program, e.g. libraries, mocks, neither tests.
General Guidelines
All unit test files should be named using the name of the module (e.g.: MyFile.js) + "Tests.js" (e.g.: MyFileTests.js).
Running Unit Tests
From the command line:
yarn test
: The default test command that will execute the unit tests using PhantomJS.yarn test-chrome
: Executes the unit tests in a chrome browser.yarn test-chrome-debug
: Executes the unit tests in a chrome browser- clicking debug button, then press
F12
. Your code is continuously running in this mode. - Refresh the debug page to trigger your
debugger
statement or breakpoints again. - Make a code change, close the debug tab, and reopen the debug page (by clicking the debug button) to execute your new code without having to run
yarn test
again.
- clicking debug button, then press
yarn
executes the command which is specified in package.json
, see below:
"scripts": {
"test": "node scripts/testUnit.js",
"test-chrome": "node scripts/testUnit.js --type chrome",
"test-chrome-debug": "node scripts/testUnit.js --type chrome-debug",
"test-sauce": "node scripts/testUnit.js --type sauce",
"test-tdd": "node scripts/testUnit.js --type tdd"
},
Karma v1.4.1 server started at http://0.0.0.0:9876/
INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: Connected on socket Ws_QWo5 with id 258
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 82 of 82 SUCCESS
Karma has exited with 0