Node Unit Tests

From Logic Wiki
Jump to: navigation, search

Mocha is unit testing environment for javascripts and Chai is an assertation library

installation

npm install --save-dev mocha
npm install --save-dev chai

Create tests folder in root project folder

var expect = require('chai').expect;

it('expects true to equal true', function(){
  expect(true).to.equal(false);
});

Webstorm & Node

Preferences > Node.js and NPM > Coding assistance for Node.js -> check

Preferences > Language & Frameworks > Javascript > Libraries -> Download mocha & chai

Run > Edit Configurations > + > Mocha -> name: Mocha Tests & type tests to Test directory

Run tests from command line

from where the mocha bin folder reference it

mocha tests

tests is folder name here.

The sub-directories will automatically be searched if the "--recursive" command line parameter is specified

Test Suites

Test suites allow you to group similar tests together.

Tests suites are defined in Mocha with the “describe” API call.

var expect=require(‘chai’).expect

describe(‘test_suite’, function(){
  it(‘returns true’, function(){
     expect(call()).to.equal(true);
  });
});

Hooks

Before and After Hooks

  • The Mocha ‘Before’ API call specifies code that executes before any test contained in a Describe block.
  • The Mocha ‘After’ API call specifies code that executes after all test contained in a Describe block have
  • Can be placed anywhere in the Describe block.
  • Multiple calls can be specified in any Describe block.
describe(‘test_suite’, function(){
   before(function(){
     console.log(‘Before tests’);
   });
   it(‘returns true’, function(){
     expect(call()).to.equal(true);
   });
   after(function(){
     console.log(‘After tests’);
   });
});

‘beforeEach’ and ‘afterEach’

  • The Mocha ‘beforeEach’ API call specifies code that executes before each test contained in a Describe block.
  • The Mocha ‘afterEach’ API call specifies code that executes after each test contained in a Describe block
  • Can specify multiple and can be placed anywhere in the Describe block
describe(‘test_suite’, function(){   
  beforeEach(function(){     
    console.log(‘before each’);   
  });
  it(‘returns true’, function(){  
      expect(call()).to.equal(true); 
  }); 
  afterEach(function(){  
    console.log(‘After each’); 
  });
});

Root-level Hooks

  • Mocha has an implied describe block known as the ‘root suite’ which is outside all other test suites.
  • Any hooks specified outside of a describe block will be included in the ‘root suite’
  • Hooks in the root suite will apply to all tests found in all files.
 beforeEach(function(){  
    console.log(‘before each’); 
 }); 
 describe(‘test_suite’, function(){
   it(‘returns true’, function(){
     expect(call()).to.equal(true); 
   }); 
 });
 afterEach(function(){  
   console.log(‘After each’); 
 });

Chai Assert API

https://www.chaijs.com/api/

it('assert_example', function(){
  assert(false, "Assert Fail!");
});

Additional Assert API Calls

it("Assert types, function(){
   assert.isTrue(true, "true");
   assert.isNaN(1.1, "NaN");
   assert.exists(foo, "!Exists");
   assert.isArray(obj, "!Array");
});
assert.equal(actual, extepceted);
assert.isString(actual, extepceted);
assert.property(object, propName);
assert.throws(function);

Async

Async Testing of Callbacks

To test async code with callbacks pass a "done" parameter to your test.

 function myAsyncFunction(callback){
    setTimeout(function(){
      callback("blah");
    }, 50);
  }

it('callback test', function(done){
  myAsyncFunction(function(str){
    expect(str).to.equal("blah");
    done();
  });
});

Async Testing with Promises

  • To test async code with promises you simply return from your code test.
  • Mocha delays the test until the returned promise is resolved
function promiseFunc(){
 return new Promise(
 (resolve, reject)=>{
 setTimeout(()=>{
 resolve(“blah”);}, 50);
 });
}
it(“promise test”, function(){
 return promiseFunc().then(res=>{
 expect(res).to.equal(“blah”);
 });
});

Async Testing with Async/Await

  • To test with the async/await keywords specify “async” on your unit test.
  • Inside your test you then call “await” on the asynchronous function that you’re testing.
  • Your unit test will return a promise which Mocha will wait to be resolved.
function promiseFunc(){
 return new Promise(
 (resolve, reject)=>{
 setTimeout(()=>{
 resolve(“blah”);}, 50);
 });
}
it(“await test”, async ()=>{
 var res = await promiseFunc();
 expect(res).to.equal(“blah”);
});