question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reading request body never consumes anything when using Jest mock timers

See original GitHub issue

Bug Description

Modern Jest fake timers seem to cause problems for reading the body of Undici Requests.

Reproducible By

The following code deadlocks:

import { Request } from 'undici'

beforeEach(() => {
  const mockTimeBase = 1643948235000

  jest.useFakeTimers('modern')
  jest.setSystemTime(mockTimeBase)
})

afterEach(() => {
  jest.useRealTimers()
})

test('request and jest fake timers problem', async () => {
    const req = new Request('http://example.com', { method: 'POST', body: 'some message' })
    console.log('Awaiting undici request')
    console.log(await req.text())
    console.log('Done')
})

Expected Behavior

The request body should be consumed during reading even when using fake timers.

Logs & Screenshots

Environment

Node 17.6 Jest 27.2.5 Undici 4.14.1 (also reproduced on 4.13.0)

Additional context

Removing the use of fake timers or using the Node Request builtin (i.e. comment out the first line) causes the test to complete as expected. If you convert the .text() invocation to instead call read() on the reader for the body, read() always returns 'some message' as the value and done is always false. Jest fake timers say they impact queueMicrotask & I noticed the streams in undici use queueMicrotask which may be related to the problem. Even if there’s no way to strictly fix this within undici, it would be nice for there to be an escape hatch of some kind (e.g. some callback I can register to call jest.runAllTimers probably fixes the issue)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
aduh95commented, Feb 27, 2022

FWIW queueMicrotask is not available in primordials (it’s not an API provided by V8, Node.js does its own implementation). I think we could definitely expose it, maybe as import { queueMicrotask } from 'node:timers/promises'.

1reaction
mcollinacommented, Feb 25, 2022

An option is to completely bypass jest fake objects and use Node primordials (I’m probably surprised I’m saying this). We might consider exposing them (cc @aduh95).

A PR to fix this would be welcomed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest: Timer and Promise don't work well. (setTimeout and ...
Timer Mocks work by replacing functions like setTimeout() with mocks when jest.useFakeTimers() is called. These mocks record the arguments ...
Read more >
Timer Mocks
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation of setTimeout() ...
Read more >
Problems with using useFakeTimers('modern') in a create- ...
Tagged with jest, javascript, react, testing. ... the documentation and trying to use something like this to skip the debounce timer:.
Read more >
Using Fake Timers
In some cases, when your code uses timers ( setTimeout , setInterval , clearTimeout , clearInterval ), your tests may become unpredictable, slow ......
Read more >
Mocking | Guide
Vitest uses @sinonjs/fake-timers package for manipulating timers, ... (or mocked) version of a function we can use vi.fn() (read more here).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found