Jest: setTimeout is not promisifiable

Created on 18 Feb 2018  ·  4Comments  ·  Source: facebook/jest

The simple testcase

const {promisify} = require('util');
const timeout = promisify(setTimeout);

test('should timeout', async () => {
    await timeout(100);
});

works in Node environment, but fails in JSDOM. The difference is that JSDOM gives custom setTimeout implementation, that doesn't work with Node built-in util.promisify.

Is there a way to make this work? For example, get the original Node's setTimeout within JSDOM.

Most helpful comment

If this is not a bug, then it should show a different error message. Currently it just fails due to test duration timeout.

  ● should timeout

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

      at node_modules/jest-jasmine2/build/queue_runner.js:72:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)

Maybe if people will find this issue, it already would give them a pointer where to look.

All 4 comments

Hey @just-boris, this issue tracker is not a help forum. We recommend using our discord channel or StackOverflow where there is an active jestjs tag for questions

For this question though you may have better luck asking the JSDOM community

Hello @rickhanlonii,

The behavior that I reported is a bug, not a question. The code sample works differently in Node and JSDOM environments, and this is not like it was intended.

It is not a bug - it's by design that you get a setTimeout like the one in the browser. It's easy enough to promisify manually, or you can use fake timers.

If you really want to do this, for whatever reason, you can create a custom environment inheriting from JSDOMEnvironment and assign it to global there. But it won't be supported out of the box in Jest as it breaks the abstraction.

If this is not a bug, then it should show a different error message. Currently it just fails due to test duration timeout.

  ● should timeout

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

      at node_modules/jest-jasmine2/build/queue_runner.js:72:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)

Maybe if people will find this issue, it already would give them a pointer where to look.

Was this page helpful?
0 / 5 - 0 ratings