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 27: modern faketimers and jest.setSystemTime #11440

Closed
thernstig opened this issue May 24, 2021 · 6 comments
Closed

Jest 27: modern faketimers and jest.setSystemTime #11440

thernstig opened this issue May 24, 2021 · 6 comments

Comments

@thernstig
Copy link
Contributor

🐛 Bug Report

Using modern fake (explicitly or implicitly) in Jest 27 together with jest.setSystemTime gives:

TypeError: setSystemTime is not available when not using modern timers

To Reproduce

jest.useFakeTimers('modern');
jest.setSystemTime(new Date('2020-10-01T06:40:38Z'));

Expected behavior

For it to not complain.

Link to repl or repo (highly encouraged)

envinfo

  System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
  Binaries:
    Node: 14.16.0 - ~/.local/share/nvm/v14.16.0/bin/node
    npm: 6.14.11 - ~/.local/share/nvm/v14.16.0/bin/npm
  npmPackages:
    jest: 26.6.3 => 26.6.3
@SimenB
Copy link
Member

SimenB commented May 25, 2021

That snippet runs fine. Can you post a full reproduction?

@thernstig
Copy link
Contributor Author

Tested with Jest 27.0.1

// jest.config.js
export default {
  testEnvironment: 'node',
  testRunner: 'jest-circus/runner',
  transform: {},
};
// unit.test.js
it('is awesome', async () => {
  jest.useFakeTimers('modern');
  jest.setSystemTime(new Date('2020-10-01T06:40:38Z'));
  expect(true).toBe(true);
});
> jest unit.test.js
 FAIL  ./unit.test.js
  ✕ is awesome (3 ms)

  ● is awesome

    TypeError: setSystemTime is not available when not using modern timers

      1 | it('is awesome', async () => {
      2 |   jest.useFakeTimers('modern');
    > 3 |   jest.setSystemTime(new Date('2020-10-01T06:40:38Z'));
        |        ^
      4 |   expect(true).toBe(true);
      5 | });
      6 |

      at Object.setSystemTime (../node_modules/@jest/core/node_modules/jest-runtime/build/index.js:1989:17)
      at Object.<anonymous> (unit.test.js:3:8)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.268 s, estimated 1 s
Ran all test suites matching /unit.test.js/i.

@thernstig
Copy link
Contributor Author

Ok there is something else at play here, since I tried the same in a fresh repo and could not reproduce it. I will need to track down in the main repo what the problem might be.

@thernstig
Copy link
Contributor Author

This can be closed, sorry for taking your time. Hope it might help some future user.

The problem was that the package jest-playwright-preset included Jest as a normal dependency and not as a peer dependency, see playwright-community/jest-playwright#665

Since it has been fixed in "jest-playwright-preset": "1.6.0-rc4" I urge everyone to update. Hopefully they will release a major release soon.

@SimenB
Copy link
Member

SimenB commented May 25, 2021

No worries, glad to hear you figured it out 👍

@github-actions
Copy link

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 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants