Node - Nodeunit

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > Javascript - Test

1 - About

Advertising

3 - Getting started

3.1 - 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

3.2 - 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.

3.3 - 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();
};

3.4 - Run it

3.4.1 - 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)                                                                            
Advertising

3.4.2 - Intellij

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

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

4 - Documentation / Reference

web/javascript/test/nodeunit.txt · Last modified: 2019/02/16 17:40 by gerardnico