Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest unit tests hang when using jest.useFakeTimers #555

Closed
2 tasks done
rburgst opened this issue Nov 9, 2021 · 8 comments
Closed
2 tasks done

jest unit tests hang when using jest.useFakeTimers #555

rburgst opened this issue Nov 9, 2021 · 8 comments

Comments

@rburgst
Copy link

rburgst commented Nov 9, 2021

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.23.1

Plugin version

No response

Node.js version

14.18

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

Monterey

Description

When using fastify in a jest unit test calling

    jest
      .useFakeTimers("modern")
      .setSystemTime(new Date("2020-02-08T20:10:21.520Z"));

causes the server.inject call to stall forever.

Steps to Reproduce

see repro repo: https://github.com/rburgst/fastify-jest-timers

Expected Behavior

it should be possible to mock the current system time using jest.useFakeTimers().setSystemTime(...)

@mcollina
Copy link
Member

mcollina commented Nov 9, 2021

I don't know why this is happening. It's likely because of Jest own environment.

I personally won't have time to work on this, but a PR would be highly appreciated.

(Generically I do not recommend to use Jest for testing Node.js applications)

@mcollina mcollina transferred this issue from fastify/fastify Nov 9, 2021
@mcollina
Copy link
Member

mcollina commented Nov 9, 2021

I moved this issue to help.

@jsumners
Copy link
Member

jsumners commented Nov 9, 2021

Please file an issue with Jest. They create a new environment. We target Node.js.

@rburgst
Copy link
Author

rburgst commented Nov 9, 2021

just for the record, we have countless repos with both fastify and jest and so far this combination has worked pretty great. So I am quite surprised about the statement

(Generically I do not recommend to use Jest for testing Node.js applications)

@mcollina do you have further details on why this would be the case?

@mcollina
Copy link
Member

mcollina commented Nov 9, 2021

jestjs/jest#2549 and a lot of other issues. To be honest it works fine 99% of the time but that remaining 1% keeps biting teams across the globe and they spend days trying to understand why things do not work as they expect to work as they do when running Node.js proper.


Regarding your problem, I think you are missing a call to run all the pending timers: https://jestjs.io/docs/timer-mocks.

@rburgst
Copy link
Author

rburgst commented Nov 9, 2021

hmm, for the record, using
https://www.npmjs.com/package/jest-environment-node-single-context
seems to work as suggested in jestjs/jest#2549 (comment)

@kibertoad
Copy link
Member

kibertoad commented Nov 8, 2022

This seems to be related to plugin registration somehow.
The part that hangs is this: await fastify.ready(), and if I add fake timer increment somewhere in between, fastify.after block is invoked without plugins having been properly registered.

It also hangs if one does await fastify.register(plugin)

@onosendi
Copy link

onosendi commented Jan 3, 2023

It seems jest.useFakeTimers({ advanceTimers: true }) fixes the issue. Not really sure why, though.

From the docs:

  /**
   * If set to `true` all timers will be advanced automatically by 20 milliseconds
   * every 20 milliseconds. A custom time delta may be provided by passing a number.
   * The default is `false`.
   */
  advanceTimers?: boolean | number;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants