Verify that a function calls another function using Mocha

I have a question about testing a specific situation in Mocha for Node.js. Suppose my application had the following code ...

function a() {
     //...
}

function b() {
    //...
}

function c() {
    if(condition) {
         a();
    } else {
         b();
    }
}

If I were to test function c, how could I check if function a or function b was called? Is there any way to do this?

+5
source share
2 answers

I found a solution for what I was trying to do. Sinon spies can help determine if a function has been called or not.

+5
source

This is what code coverage means . Fortunately, mocha supports this using JSCoverage . I am using a MakeFile that looks like this:

coverage:
    rm -rf lib-cov
    jscoverage --no-highlight lib lib-cov
    @MOCHA_COV=1 mocha --reporter html-cov > coverage.html
    google-chrome coverage.html
  • () javascript (), Mocha .
  • jscoverage lib-cov lib.
  • , , node , .
  • , coverage.html google-chrome.

mocha , :

var BASE_PATH   = process.env.MOCHA_COV ? './../lib-cov/' : './../lib/';

, MOCHA_COV=1 .


:

+1

All Articles