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.

[Bug]: Jest 27 - useFakeTimers erases performance.mark and friends

See original GitHub issue

Version

27.3.1

Steps to reproduce

window.performance.mark = () => {}
jest.useFakeTimers()
assert(typeof window.performance.mark === 'undefined')

jest.useRealTimers()
assert(typeof window.performance.mark === 'function')

jest.useFakeTimers("legacy")
assert(typeof window.performance.mark === 'function')

Expected behavior

jest.useFakeTimers() to not override global polyfills/mocks of performance.mark

Actual behavior

jest.useFakeTimers() overrides global polyfills/mocks of performance.mark, and sets it to undefined.

Additional context

I found a similar issue in @sinonjs/fake-timers https://github.com/sinonjs/fake-timers/issues/136. Don’t know if it’s related to jest though.

Environment

System:
    OS: macOS 12.0.1
    CPU: (10) arm64 Apple M1 Pro
Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: 3.0.0-rc.9 - /opt/homebrew/bin/yarn
    npm: 8.1.0 - /usr/local/bin/npm

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
vovacodescommented, Nov 10, 2021

The use-case is pretty straight-forward:

  • I have some code that does performance.mark() somewhere deeply in its implementation
  • I want to test some parts of this code and I don’t care about performance.mark() and I’m fine with it just being a noop.
  • I put a “global” stub for performance.mark() in my setupFile.js
  • I expect that performance.mark() always will be around so my tests don’t crash.

I’d be ok with any way of making it happen, even things like jest.useFakeTimers({ ignore: [window.performance] }) or whatever

2reactions
fatso83commented, Dec 23, 2021

I think this issue could be fixed pretty easily be just making avoiding to fake performance. This is already possible by telling @sinonjs/fake-timers, but we have just forgot to update our docs … 🙈 Ref https://github.com/sinonjs/fake-timers/issues/409, https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56961#pullrequestreview-797296492 and https://github.com/sinonjs/fake-timers/issues/374.

@SimenB I am not to well versed in Jest’s use of fake timers, but I do not see any way of passing options to customise its timer faking. If we could exclude performance this issue would go away. p.s. Thanks for the ☕

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timer Mocks - Jest
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation of setTimeout() ...
Read more >
Fix the "not wrapped in act(...)" warning with Jest fake timers
... to manually use act to avoid missing bugs in your code. In this lesson you'll learn how to identify the cause of...
Read more >
mongoose | Yarn - Package Manager
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports Node.js and Deno (alpha). Build Status NPM...
Read more >
Hands-On way to build Frontend with React | Nick Ovchinnikov
First will be a configuration of the ReactJS application with Typescript and using build tools Webpack+Babel. Second, there're presented TDD or TLD approaches ......
Read more >
Test-Driven React - Library
Code, Jest, Babel, ESLint, Webpack, Enzyme, and many other tools. ... than revealing potential bugs that might occur under edge case conditions.
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