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

[Bug]: doNotFake option for useFakeTimers is ignored when it contains all available options #15374

Open
ensconced opened this issue Nov 10, 2024 · 1 comment

Comments

@ensconced
Copy link

ensconced commented Nov 10, 2024

Version

29.7.0

Steps to reproduce

Steps:

  1. Clone my repo at https://github.com/ensconced/jest-do-not-fake-bug
  2. npm install
  3. npm test

Expected behavior

Both tests should pass.

Actual behavior

One of the tests times out.

Additional context

It seems like when you include all the available options in doNotFake, like this:

  jest.useFakeTimers({
    doNotFake: [
      "Date",
      "hrtime",
      "nextTick",
      "performance",
      "queueMicrotask",
      "requestAnimationFrame",
      "cancelAnimationFrame",
      "requestIdleCallback",
      "cancelIdleCallback",
      "setImmediate",
      "clearImmediate",
      "setInterval",
      "clearInterval",
      "setTimeout",
      "clearTimeout",
    ],
  });

...it's equivalent to doing this:

  jest.useFakeTimers();

...whereas it should be equivalent to not faking timers at all.

Environment

System:
    OS: macOS 14.4
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 20.9.0 - ~/.local/state/fnm_multishells/83672_1731248039013/bin/node
    npm: 10.1.0 - ~/.local/state/fnm_multishells/83672_1731248039013/bin/npm
    pnpm: 9.9.0 - /opt/homebrew/bin/pnpm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@ensconced
Copy link
Author

Perhaps a simpler demonstration of the issue would be this:

const originalSetTimeout = setTimeout;

it("this passes", async () => {
  jest.useFakeTimers({ doNotFake: ["setTimeout"] });
  expect(setTimeout).toBe(originalSetTimeout);
});

it("this should also pass, but it fails", async () => {
  jest.useFakeTimers({
    doNotFake: [
      "Date",
      "hrtime",
      "nextTick",
      "performance",
      "queueMicrotask",
      "requestAnimationFrame",
      "cancelAnimationFrame",
      "requestIdleCallback",
      "cancelIdleCallback",
      "setImmediate",
      "clearImmediate",
      "setInterval",
      "clearInterval",
      "setTimeout",
      "clearTimeout",
    ],
  });
  expect(setTimeout).toBe(originalSetTimeout);
});

@ensconced ensconced changed the title [Bug]: doNotFake is ignored when it contains all available options [Bug]: doNotFake option for useFakeTimers is ignored when it contains all available options Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant