1

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

8
  • Is it because you're setting the src before attaching the onload event? Since you're setting the source from an encoded string, it might "load" it synchronously, so your event handler never fires. Commented Feb 22, 2018 at 13:41
  • I don't see any assertions in the test. Jest might be erroring because it's expecting an assertion, but an assertion isn't being done. Unless that's what checkBase64 is doing? Commented Feb 22, 2018 at 13:41
  • @JamesThorpe, I've updated my code (in my editor and here) with no success. I get the same error Commented Feb 22, 2018 at 13:43
  • Could you show the source of checkBase64? @IvanPrizov Commented Feb 22, 2018 at 13:49
  • @kingdaro The first code block in my question is checkBase64. I'm using a default export Commented Feb 22, 2018 at 13:50

1 Answer 1

2

The Image constructor only exists in the DOM, and Jest tests are run in Node. It looks like the promise returned from the checkBase64 function is failing silently when trying to access Image. You'll need to mock it, either with something minimal (global.Image = ...) or something fully-featured like jsdom.

Sign up to request clarification or add additional context in comments.

5 Comments

I've tried both of your suggestions, but they didn't help. When I was trying to use expect.assertions(2) I got an error Expected two assertions to be called but received zero assertion calls. BTW, jest tests work fine without expect or done. They just get passed
I edited my answer with an example of how expect.assertions should be used. The error message you're giving me tells me that the checkBase64 function might not actually resolve. What happens if you console.log(result)?
Also... don't jest tests run in node? Image doesn't exist in node. If that's the issue, I'll rewrite my answer.
Ou... I guess that's the issue. I mean, I didn't really know that image doesn't exist in node
I see. I suspect the promise is silently failing, then. Editing my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.