Node - Nodeunit

About

Javascript - Test Framework

Getting started

Install Nodeunit

npm install nodeunit -g
C:\Users\gerard\AppData\Roaming\npm\nodeunit -> C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\bin\nodeunit + [email protected]
added 334 packages in 58.521s

Environment

  • Make sure that NODE_PATH environment variable points to the parent directory of nodeunit module.
  • Create a test folder as a test source folder
mkdir $root_project/test 
  • Optional: In Intellij, mark it as test dir.

Test script

Create the example.js file

exports.testSomething = function(test) {
    test.expect(1);
    test.ok(true, "this assertion should pass");
    test.done();
};

exports.testSomethingElse = function(test) {
    test.ok(false, "this assertion should fail");
    test.done();
};

Run it

Manually

nodeunit example.js
example.js                                                                                                       
nico                                                                                                             
√ testSomething                                                                                                  
× testSomethingElse                                                                                              
                                                                                                                 
Assertion Message: this assertion should fail                                                                    
AssertionError: false == true                                                                                    
    at Object.ok (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\types.js:83:39)                  
    at Object.exports.testSomethingElse (D:\git_js\javascript\test\example.js:9:10)                              
    at Object.<anonymous> (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:236:16)         
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:236:16                              
    at Object.exports.runTest (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:70:9)       
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:118:25                              
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:513:13                            
    at iterate (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:123:13)                  
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:134:25                            
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:515:17                            
    at Immediate.<anonymous> (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\types.js:146:17)     
    at runCallback (timers.js:672:20)                                                                            
    at tryOnImmediate (timers.js:645:5)                                                                          
    at processImmediate [as _immediateCallback] (timers.js:617:5)                                                
                                                                                                                 
                                                                                                                 
FAILURES: 1/2 assertions failed (0ms)                                                                            

Intellij

In Intellij Create a Nodeunit run configuration (of type Nodeunit) Nodeunit Run Configuration Intellij

Result:

D:\nodejs\node.exe C:\Users\gerard\.IntelliJIdea2017.2\config\plugins\NodeJS\js\nodeunit\nodeunit-intellij-starter.js C:/Users/gerard/AppData/Roaming/npm/node_modules/nodeunit javascript/test/example.js
Testing started at 16:23 ...

this assertion should fail
Expected :true
Actual   :false
 <Click to see difference>

AssertionError: false == true
    at Object.ok (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\types.js:83:39)
    at Object.exports.testSomethingElse (D:\git_js\javascript\test\example.js:8:10)
    at Object.<anonymous> (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:236:16)
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:236:16
    at Object.exports.runTest (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:70:9)
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\core.js:118:25
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:513:13
    at iterate (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:123:13)
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:134:25
    at C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\deps\async.js:515:17
    at Immediate.<anonymous> (C:\Users\gerard\AppData\Roaming\npm\node_modules\nodeunit\lib\types.js:146:17)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

Process finished with exit code 0

Documentation / Reference





Discover More
Node Plugin Idea
IDEA - Node

Node configuration inside IDEA Node is not installed by default. You need to install it as a plugin. The node_module folder should be marked as a library. To enable code completion for Node.js...
Javascript Test - Runner

in Javascript. See also: Winner: Jest because: it's fully integrated in my IDE it supports React out of the box it can also test Typescript it forces you to not start a browser to test...



Share this page:
Follow us:
Task Runner