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

Setting timers config property to modern does not use modern mode by default in test files #11370

Closed
blimmer opened this issue May 3, 2021 · 5 comments · Fixed by #11376
Closed

Comments

@blimmer
Copy link
Contributor

blimmer commented May 3, 2021

🐛 Bug Report

My understanding of the timers config property (docs) is that setting the value to modern should automatically use "modern" mode everytime I call jest.useFakeTimers(). However, that doesn't appear to be the case.

I receive this error when trying to call modern-specific methods:

  ● bug report › should allow using setSystemTime because timers is set to 'modern' in config

    TypeError: setSystemTime is not available when not using modern timers

      1 | describe('bug report', () => {
      2 |   it("should allow using setSystemTime because timers is set to 'modern' in config", () => {
    > 3 |     jest.useFakeTimers().setSystemTime(0); // This does not work, but should based on the jest.config.js `timers` setting
        |                          ^
      4 |     jest.useFakeTimers('modern').setSystemTime(0); // This does work
      5 |   })
      6 | })

      at Object.setSystemTime (node_modules/jest-runtime/build/index.js:1856:17)
      at Object.<anonymous> (bug-report.test.js:3:26)

To Reproduce

Steps to reproduce the behavior:

  1. Set timers in jest.config.js to modern
  2. Try to call jest.useFakeTimers().setSystemTime(0); in a test file.

An error is thrown when running the test file.

Expected behavior

Because I set timers to modern in jest.config.js, I expected to be able to call jest.useFakeTimers().setSystemTime(0); because modern should be the default based on my configuration.

Link to repl or repo (highly encouraged)

https://replit.com/@blimmer/jest-playground#bug-report.test.js

envinfo

  System:
    OS: macOS 11.2.3
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 12.21.0 - ~/.nvm/versions/node/v12.21.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v12.21.0/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3
@SimenB
Copy link
Member

SimenB commented May 4, 2021

Setting timer in docs means you're always using fake timers. If you later call jest.useFakeTimers() you "override" that setting, and if you want to use modern timers you need to pass 'modern'. I agree this is no clear in the docs - would you be up to sending a PR specifying?

Note that in Jest 27 the default is flipped, so wording in docs/Configuration.md should be different from the ones in versioned_docs.

@blimmer
Copy link
Contributor Author

blimmer commented May 4, 2021

Oh, interesting. Is there a way to call setSystemTime when you've faked timers via the config?

The only way I know how to do it is by chaining the call to useFakeTimers.

@SimenB
Copy link
Member

SimenB commented May 5, 2021

Just calling jest.setSystemTime should be fine - the chain is jest, not a returned fake timer implementation

blimmer added a commit to blimmer/jest that referenced this issue May 5, 2021
blimmer added a commit to blimmer/jest that referenced this issue May 5, 2021
@blimmer
Copy link
Contributor Author

blimmer commented May 5, 2021

Cool - thanks. I opened #11376 with a documentation update.

@github-actions
Copy link

github-actions bot commented Jun 6, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants