I've stumbled across a problem of testing the asynchronous code.
Here's the code of my function:
export default base64String =>
new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => {
const dimensions = {
width: image.width,
height: image.height,
};
resolve(dimensions);
};
image.onerror = err => reject(err);
image.src = base64String;
});
It takes in a base64 encoded string and returns width and height of an image;
The test looks the following:
import checkBase64 from '../src/helpers/check-base64';
import base64String from './base64String';
test('should return width and height of an image in base64', async () => {
const result = await checkBase64(base64String);
});
The problem is that the test fails with the error:
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
I was following jest docs and some stack overflow questions but neither of them helped
srcbefore attaching theonloadevent? Since you're setting the source from an encoded string, it might "load" it synchronously, so your event handler never fires.checkBase64? @IvanPrizovcheckBase64. I'm using a default export