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.

jest.useRealTimers() is not a function when running RTL tests in jest-electron environment

See original GitHub issue

I decided to try using jest-electron environment in our jest test setup because jsdom environment has some missing browser functionality that we were using.

Relevant code or config:

pasting only necessary changes here of my jest.config.js

{
 .
 .
  runner: 'jest-electron/runner',
  testEnvironment: 'jest-electron/environment',
  .
  .
  .
}

What you did:

ran npm run test

What happened:

i think helpers file inside RTL /dom is calling jest.useFakeTimers()… which is not available in jest-electron environment.

The error that i’m getting is shown in the following SS. Screenshot 2021-02-27 at 1 16 00 PM

Reproduction:

Small repro can be created on local machine on following these steps. clone : https://github.com/tmtsoftware/esw-ocs-eng-ui git checkout jest-branch npm i & npm t

Problem description:

RTL/jest-dom helpers are calling jest.timers functionality without checking jest-environment.

Suggested solution:

there could be some kind of check related to which environment jest is currently running before this line. @Testing-Library/dom/dist/helpers.js:38.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ZheyangSongcommented, Mar 25, 2021

In case this helps anyone facing the same issue, this breakage is seemly introduced by a patch version of 7.29. You can try to downgrade this library to a 7.28.x or lower. You should get this issue gone.

Hope this will get the testing-library project team’s attention as well. For me, the module I use, its custom environment doesn’t define userRealTimers. https://github.com/facebook-atom/jest-electron-runner/blob/master/packages/electron/src/Environment.js#L16

1reaction
Rulukcommented, Apr 27, 2021

For the record, the same thing is happening to me when using the latest RTL in CodeSandbox.

Test code:

import React from 'react';

import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import App from './App';

describe('App', () => {
  test('it should render', () => {
    render(<App />);
  });
});

Dependencies:

{
    "@testing-library/react": "11.2.6",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-scripts": "4.0.0"
}

EDIT: New fact. It seems that it only fails with RTL 11.2.6. With 11.2.5 and below it seems to work fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFakeTimers not working in jest/testing-library
You should advance timers after rendering the component. Besides, you should call jest.advanceTimersByTime() inside act function.
Read more >
Timer Mocks - Jest
The native timer functions (i.e., setTimeout(), setInterval(), clearTimeout(), clearInterval()) are less than ideal for a testing ...
Read more >
JavaScript and TypeScript Tests - Datadog Docs
Custom environments like @jest-runner/electron/environment in ... and not the underlying worker node where the Datadog Agent is running.
Read more >
Add Testing to Your React TypeScript Project using Jest with 4 ...
Integrating Jest into a TypeScript project. ... cd react-playbook/packages/typescript-jest npm install npm run type-check npm test ...
Read more >
Using React Testing Library and Jest with AG Grid
What is the Testing Library? How to Run Testing Library Tests with NPM; How to use The Testing Library; Testing the Grid Component...
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