Jest: Different behaviour when using util.promisify

Created on 20 Aug 2017  路  13Comments  路  Source: facebook/jest


Do you want to request a feature or report a bug?

bug

What is the current behavior?

test('should return 1', async () => {
  const fd = await open('src', 'r')

  const buffer = Buffer.alloc(1)

  const { bytesRead } = await read(fd, buffer, 0, 1, 0)

  expect(bytesRead).toEqual(1)
})

Received: undefined

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

https://repl.it/KR7e

What is the expected behavior?

Receive 1 instead of undefined

node.js doc

If this method is invoked as its util.promisify()ed version, it returns a Promise for an object with bytesRead and buffer properties.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest is 20.0.4. Node: 8.4.0, Npm: 5.3.0, OS: macOS Sierra

Needs Repro

Most helpful comment

@Rokt33r your issue is #2549. In particular this line is false meaning the callback passed as options is not assigned to cb.

I opened up https://github.com/salesforce/tough-cookie/pull/158 which fixes the issue and makes your test work correctly in Jest.


util.promisify works correctly, so I'll close this issue

All 13 comments

Node 8.1.3, Windows 10 x64, Jest 20.0.4, reproduced.

For now, it would be helpful to throw a warning that util.promisify may not function properly in jest tests...

The reproduction in the repl.it in the OP does not reproduce the wrong behavior on the latest version of Jest. The test now fails with this error, which seems expected?:

image

The linked issue from got works fine if I remove micro, which I've never heard of and have no idea what does. And it just times out if I do not remove it, it doesn't fail like the linked issue says it does

image

Can someone put together a reproduction that fails with Jest 24.8.0?

Deleting micro doesn't work for me. I'm using [email protected] and [email protected].

Screen Shot 2019-05-28 at 15 36 33

And I don't think micro causes the problem. It just handles several cases that its request handler is returning or throwing something without calling res.end. https://github.com/zeit/micro/blob/master/packages/micro/lib/index.js#L25

Can you put together a reproduction I can pull down and run?

Oh, make sure you are using the node environment, and not the jsdom one: https://jestjs.io/docs/en/configuration#testenvironment-string

I'm using node env.

{
    "preset": "ts-jest",
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/dist/"
    ]
}

I'll make a repo soon.

@SimenB https://github.com/Rokt33r/jest-repro-promisify Sorry for being late.

@SimenB Ping.

@Rokt33r your issue is #2549. In particular this line is false meaning the callback passed as options is not assigned to cb.

I opened up https://github.com/salesforce/tough-cookie/pull/158 which fixes the issue and makes your test work correctly in Jest.


util.promisify works correctly, so I'll close this issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenlautier picture stephenlautier  路  3Comments

jardakotesovec picture jardakotesovec  路  3Comments

nsand picture nsand  路  3Comments

Secretmapper picture Secretmapper  路  3Comments

kgowru picture kgowru  路  3Comments