vendredi 24 juillet 2015

How to force inner invoked function to throw exception with Mocha/Chai

I want to test the function B to catch exception thrown from function A with Mocha/Chai.

function A() {
  // 1. the third party API is called here
  // some exception may be thrown from it
  ...
  // 2. some exception could be thrown here
  // caused by the logic in this function
}

function B() {
  // To catch exception thrown by A()
  try {
     A();
  } catch(err) {
     console.error(err);
  }
  ...
}

I want force A to throw exception, while doing the test of B. So I can make sure the function B catch the exception from A correctly.


After searching some posts:

Force Mocha test to fail?

Testing JS exceptions with Mocha/Chai

I did not find the correct answer.


Is my question reasonable? if it is, how to do that test with Mocha/Chai?

Aucun commentaire:

Enregistrer un commentaire