mocha test when callback is run

353 views Asked by At

I am testing an api which already has a callback inside a callback at the end of the function. I want to wrap this in a test to verify the object is correct but this doesn't seem to be working. callbackEnd() gets called but that's it.

In the library on a script load success:

function callback() {
  // populate gpt object
  if(typeof callbackEnd === 'function') {
    callbackEnd();
  }
}

Mocha.js test:

"use strict";
(function() {

  describe("Callback Success", function() {
    function callbackEnd() {

      console.log('callbackEnd() called');

      it('GPT returned advars', function() {
        expect(Object.keys(someobj).length).to.beGreaterThan(0);
        console.log('GPT loaded successfully, ' + Object.keys(someobj).length);
      });

    }
  });

})();
1

There are 1 answers

0
user1572796 On

There it goes, describe -> it -> custom callback function -> done();

 "use strict";
(function() {

  describe("Callback Success", function() {

      it('GPT returned advars', function(done) {

        function callbackEnd() {
          expect(Object.keys(someobj).length).to.not.equal(0);
          console.log('GPT loaded successfully, ' + Object.keys(someobj).length);
          done();
        }

      });
  });
})();