Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use mocha for unit testing in node?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 131
    Comment on it

    mocha is a javascript test framework. It works well for both node.js and browser. It makes asynchronous testing simple and easy. Mocha test run serially. mocha is open source.

    Installing mocha globally:

    npm install -g mocha

    With the above installation, mocha runs with commands:

    mocha test


    But, I like to run my mocha test with command npm test. To do this, add the below lines of code in your package.json:

    "scripts": {
      "test": "mocha test"
    }


    then

    npm run


    Creating/Running test cases:
     

    var assert = require('assert');
    describe('Summation', function() {
      it('success: It should return 3', function() {
          assert.equal(3, 2+1);
      });
      it('failure: It should fail.', function() {
          assert.equal(3, 2+0);
      });
    
    });


    To run, use command:

    mocha index
    
    or
    
    npm test

     

    It produces:
     

    
      Summation
         success: It should return 3
        1) failure: It should fail.
    
    
      1 passing (9ms)
      1 failing
    
      1) Summation failure: It should fail.:
    
          AssertionError: 3 == 2
          + expected - actual
    
          -3
          +2
          
          at Context.<anonymous> (index.js:7:14)
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: