How to avoid time in a test when expecting a Promise with setTimeout in Jasmine
Given I have the following code:
public returnData(): Promise<any> {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
data: true,
});
}, 2000);
});
}
Now, currently my unit test successfully do:
it('should return data', async function () {
const responseMock = {
data: true,
};
await expectAsync(component.returnData()).toBeResolvedTo(responseMock);
});
But there is an obvious problem: the test takes more than 2000ms, which is unacceptable. I want to be able to test this without the need of waiting the time. I've tried many different solutions, like the following:
it('should return data', async function () {
jasmine.clock().install();
const responseMock = {
data: true,
};
setTimeout(() => {
expectAsync(component.returnData()).toBeResolvedTo(responseMock);
jasmine.clock().tick(2000);
}, 2000);
jasmine.clock().uninstall();
});
The test above pass with great timing, but when I change the responseMock
value for anything else, it still pass, so it's not really working...
What's the key to test this specific case, without the need of waiting the setTimeout time?
Why are you running the expectations in a setTimeout
? Maybe we don't need that.
Try this:
it('should return data', async function () {
jasmine.clock().install();
const responseMock = {
data: true,
};
// call the method that returns a promise and have a handle on its promise
const promise = component.returnData();
// make 2 seconds pass in a fake way
jasmine.clock().tick(2000);
// await the promise
const result = await promise;
// assert the result
expect(result).toEqual(responseMock);
jasmine.clock().uninstall();
});
I see that you're using component.
and most likely you're using Angular. If you're using Angular, I use fakeAsync/tick
for time and promises.
Check out fakeAsync/tick
here.
//
it('should return data', fakeAsync(() => {
const responseMock = {
data: true,
};
// call the method that returns a promise and have a handle on its promise
const promise = component.returnData();
// make 2000 pass in a fake way
tick(2000);
promise.then(result => {
expect(result).toEqual(responseMock);
expect(1).toBe(2); // delete this - make sure the test fails to know we made it here
});
}));