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
'.
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.
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.
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.
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.
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.
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.
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.
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();
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With