Is it possible to manipulate time through jasmine.clock() when testing asynchronous functions?

Good time of day.
Master manual on BDD with Jasmine.
There is a wonderful opportunity to test a function like setTimeout() using jasmine.clock(), but in synchronous mode.
Is it possible to manipulate time through jasmine.clock() when testing asynchronous functions when used with a call to done()?

Here, for example, the following code based on examples of the tutorial. Can I use jasmine.clock() to reduce the waiting call Kolpakov and, if possible, how to do it correctly?

describe("long asynchronous specs", function() {
 var value;

 beforeEach(function(done) {
jasmine.clock().install();
});

 beforeEach(function(done) {
 value = 0;
done();
 }, 10000);
 jasmine.clock().tick(10001);// to not wait for 10 s in the previous block beforeEach
 it("takes a long time", function(done) {
 setTimeout(function() {
value++;
expect(value).toBeGreaterThan(0);
done();
 }, 9000);
//jasmine.clock().tick(9001);
 }, 10000);
 jasmine.clock().tick(10001);// to not wait for 10 s in the previous block beforeEach
 afterEach(function(done) {
done();
 }, 1000);
 afterEach(function(done) {
 jasmine.clock().uninstall(); 
});

});
1 answer
July 9th 19 at 13:49
Solution
I think I found the answer to your question.

Anyone interested to share:
describe("clock pollution", function() {
 var value;
 it("async with clock", function(done) {
jasmine.clock().install()
 setTimeout(function() {
 value = 1;
expect(value).toBeGreaterThan(0);
jasmine.clock().uninstall()
done()
 }, 20000)
jasmine.clock().tick(20001)
})
})


Or with promises:

function delay(ms) {
 return new Promise(function(resolve, reject) {
 setTimeout(resolve, ms)
})
}

describe("clock pollution", function() {
 var value;
 it("async with clock", function(done) {
jasmine.clock().install()
delay(2000)
 .then(function() {
 value = 1;
expect(value).toBeGreaterThan(0);
jasmine.clock().uninstall()
done()
})
jasmine.clock().tick(2001)
})
})

Find more questions by tags AJAXAsynchronous programmingBehavior-driven developmentJavaScript