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.

[Dropdown Menu] Problems when testing with Jest+RTL

See original GitHub issue

Hello!

We’re trying to test a wrapper we made around the Dropdown Menu component but we found out that it’s not rendering when the trigger is clicked. This only happens in the tests, the UI works like a charm.

We thought it could be related to the portal, so we used portalled={false} and got the same results.

I tried to create a reproduction on codesandbox but now I’m getting a different error, which makes me think that maybe we have ResizeObserver mocked, I’ll have to take a look.

Failed to execute ‘observe’ on ‘ResizeObserver’: parameter 1 is not of type ‘Element’.

Here is the codesandbox reproducing the error.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

24reactions
benoitgrelardcommented, Feb 2, 2021

Hey @rubenmoya,

Thanks for reaching out. I believe this is an issue with the test environment (jest/jsdom) not having ResizeObserver. That’s why you don’t see an issue in your app. The test environment is not a real browser/DOM so you don’t necessarily have all the APIs available.

I believe what you’d have to do is mock these bits within your testing environment of choice.

We’re currently in alpha so we don’t have tests for all packages yet, but eventually you should be able to take a look at how we did it ourselves for the same purpose.

In the meantime, I’ve had a very quick go in our codebase with the same test you were trying to run, and I’ve had to mock a couple pieces for it to work:

global.ResizeObserver = class ResizeObserver {
  constructor(cb) {
    this.cb = cb;
  }
  observe() {
    this.cb([{ borderBoxSize: { inlineSize: 0, blockSize: 0 } }]);
  }
  unobserve() {}
};

global.DOMRect = {
  fromRect: () => ({ top: 0, left: 0, bottom: 0, right: 0, width: 0, height: 0 }),
};

I hope that helps, if so, feel free to close the issue if this is satisfactory.

7reactions
dkapanidiscommented, May 13, 2022

@benoitgrelard , this also worked for me on #1036 🤘 Thank you!

However, I have an extra issue created by the fact that I’m using TypeScript and it doesn’t appear to be very fond of the solution. I’m battling with it and will leave a solution here (if I find one) 😄

For anyone looking to a typescript format of the same solution:

(global as any).ResizeObserver = class ResizeObserver {
  constructor(cb: any) {
    (this as any).cb = cb;
  }

  observe() {
    (this as any).cb([{ borderBoxSize: { inlineSize: 0, blockSize: 0 } }]);
  }
  
  unobserve() { }
};

(global as any).DOMRect = {
  fromRect: () => ({ top: 0, left: 0, bottom: 0, right: 0, width: 0, height: 0 }),
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

[DropdownMenu] `jest` / `react-testing-library` tests failing on ...
Actually, any test that involves some sort of pop up box appearing currently fails (so "@radix-ui/react-dropdown-menu": "0.1.6" and "@radix-ui/ ...
Read more >
Test Drop Down With React Testing Library - Stack Overflow
When your data comes from an asynchronous fetch call, the DOM doesn't get updated synchronously, and you have to use one of the...
Read more >
How to test a Semantic UI React Dropdown using Jest and ...
A Semantic UI React Dropdown using the options array above. Give it a default value of the first option in the array, Jenny...
Read more >
Dropdown menu properties issue with sizing and color
When I use test the application on a mobile device, the dropdown menu takes up the entire screen and the "x" button to...
Read more >
React Testing Library Common Scenarios - Rafael Quintanilha
For each scenario, I describe the problem and how you can write a test for it with Jest + React Testing Library.
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