Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Asynchronous Callbacks with Jasmine

I'm using Jasmine 2.1. I am trying to use Jasmine 2.1 to test a module. One of my modules has a function that executes code asynchronously. I need to test the result of the function when the app is done executing. Is there a way to do this? Currently, my module looks like this:

var otherModule = require('otherModule');
function MyModule() {
}

MyModule.prototype.state = '';
MyModule.prototype.execute = function(callback) {
  try {
    this.state = 'Executing';
    var m = new otherModule.Execute(function(err) {
      if (err) {
        this.state = 'Error';
        if (callback) {
          callback(err);
        }
      } else {
        this.state = 'Executed';
        if (callback) {
          callback(null);
        }
      }
    });
  } catch (ex) {
    this.state = 'Exception';
    if (callback) {
      callback(ex);
    }
  }
};

module.exports = MyModule;

I am trying to test my Module with the following:

var MyModule= require('./myModule');
describe("My Module", function() {
  var myModule = new MyModule();
  it('Execute', function() {
    myModule.execute();
    expect(myModule.state).toBe('Executed');
  });
});

Clearly, the test is not awaiting for the execution to occur. How do I test an asynchronous executed function via Jasmine? In addition, am I using the state variable properly? I get lost in the asynchronous stack and I'm unsure where I can use 'this'.

like image 932
JQuery Mobile Avatar asked Dec 07 '14 16:12

JQuery Mobile


People also ask

How do you test async methods in Jasmine?

If an operation is asynchronous just because it relies on setTimeout or other time-based behavior, a good way to test it is to use Jasmine's mock clock to make it run synchronously. This type of test can be easier to write and will run faster than an asynchronous test that actually waits for time to pass.

Can callback be asynchronous?

The callback is a function that's accepted as an argument and executed by another function (the higher-order function). There are 2 kinds of callback functions: synchronous and asynchronous. The synchronous callbacks are executed at the same time as the higher-order function that uses the callback.

Do jest tests run asynchronously?

It's common in JavaScript for code to run asynchronously. When you have code that runs asynchronously, Jest needs to know when the code it is testing has completed, before it can move on to another test.

How do you test async code in Jasmine?

Jasmine supports testing async code. We have the beforeEach callback that has a setTimeout function call. We call done in the callback so that the test code is run. In the test code, the callback takes the done parameter to let us call done to indicate that the test is done.

How to handle failures with promises in Jasmine testing?

Handling failures with promises can be done like any other piece of code. If a promise is rejected in our test, then the test will fail. then the test will fail. This will also fail. We can fail a test with callbacks. Since Jasmine 3.0, the done function can detect if an Error object is passed to the function.

Why use Jasmine for automated testing?

It provides utilities that can be used to run automated tests for both synchronous and asynchronous code. Jasmine has many features such as: It’s fast and has low overhead and no external dependencies.

How to test JavaScript TimeOut functions with Jasmine?

We can use Jasmine to test JavaScript timeout functions. We can use the jasmine.clock () method to do this. to create a timerCallback spy which we can watch. We call jasmine.clock ().install () to create the Jasmine timer. And we call jasmine.clock ().uninstall () to remove it at the end. Then we call tick to change the time to what we want.


1 Answers

I would recommend taking a look at the async section of the jasmine docs. So, with this information we can use a done callback to wait for the execution to finish before testing anything, like this:

var MyModule= require('./myModule');
describe("My Module", function() {
  var myModule = new MyModule();
  it('Execute', function(done) {
    myModule.execute(function(){
        expect(myModule.state).toBe('Executed');
        done();
    });
  });
});
like image 80
Frances McMullin Avatar answered Oct 09 '22 17:10

Frances McMullin