Showing posts with label NodeJS-mocha. Show all posts
Showing posts with label NodeJS-mocha. Show all posts

Wednesday, 14 June 2017

mocha module with nodeJs tutorial

mocha module with nodeJs tutorial

Question: What is Mocha?
Mocha is a Rich JavaScript framework running on Node.js and in the browser making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.



Question: How to install Mocha?
npm install --global mocha



Question: How to test script with Mocha?
mocha tests.js



Question: How to check Success with Mocha?
Success
function add(a,b){
  return a+b;
}

   
//Success
it('should add two numbers-Success', () => {
  var res = add(33, 11);

  if (res !== 44) {
    throw new Error(`Expected 44, but got ${res}.`)
  }
});

Output
should add two numbers-Success passing (15ms)



Question: How to check Failure with Mocha?
function add(a,b){
  return a+b+2;
}

   
//Failed
it('should add two numbers-Success', () => {
  var res = add(33, 11);

  if (res !== 44) {
    throw new Error(`Expected 44, but got ${res}.`)
  }
});  


Output
1) should add two numbers-Failed 0 passing (25ms) 1 failing 1) should add two numbers-Failed: Error: Expected 44, but got 46. at Context.it (utils\utils.test.js:11:11)


Question: How to write Asynchronous code with Mocha?
describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(done);
    });
  });
});



Question: How to write Synchronous code with Mocha?
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      [1,2,3].indexOf(5).should.equal(-1);
      [1,2,3].indexOf(0).should.equal(-1);
    });
  });
});




Question: How we can achieve HOOKS with Mocha?
describe('hooks', function() {
  before(function() { });
  after(function() {  });
  beforeEach(function() {  });
  afterEach(function() {});  
});



Question: What is Github URL for mocha?
https://github.com/mochajs/mocha


Question: In Which language mocha is written?
JavaScript



Question: What is offical website URL of Mocha?
https://mochajs.org/