Sunday, September 16, 2018

NodeJS 31: UnitTesting with NodeJS - Mocha & Chai

NodeJS 31: UnitTesting with NodeJS - Mocha & Chai





########################################CODE##############################


###############################
test.js
###############################
var chai=require('chai');
var expect=chai.expect;

chai.should(); //activate assertion framework on mocha engine

function returnName(name){
return name;
}

function addNumbers(num){
return num+num;
}
function IsEven(inputVal){
return (inputval % 2)===0;
}


describe('Math Operation',function(){
    it('Returns a value added to itself',function(){
      addNumbers(5).should.equal(10);
  });
  it('Should return true for even number',function(){
    IsEven(4).should.equal(true);
    });
});

//test case
describe('1st unit test',function(){
  it('Returns the name passed',function(){
returnName('Amit').should.equal('Amit');
    });
});



###############################
app.js
###############################
module.exports=function(){
return 'hello';
}

###############################
appTest.js
###############################
const assert=require('chai').assert;
const app=require("../app");

describe('App',function(){
it('app should return hello..',function(){
assert.equal(app(),'hello');
    });
   
    it('app should return string..',function(){
assert.typeOf(app(),'string');
});

});




###############################CODE END ###############################

No comments:

Post a Comment