Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing ag-grid with @testing-library/react

I'm trying to test component which use ag-grid.

When I do assertions to check if cells with given text are rendered I receive the following error:

expect(received).toBeInTheDocument() received value must be an HTMLElement or an SVGElement. Received has type: object Received has value: HERE IS EXPECTED HTML FOR THE CELL I'VE BEEN LOOKING FOR

I have created code sandbox for this issue (see grid.test.tsx) https://codesandbox.io/s/weathered-silence-4l1s1

like image 729
Michał Avatar asked Oct 26 '25 12:10

Michał


1 Answers

It looks like ag-grid does something to the DOM. After rendering ag-grid inside the test it breaks all assertion which check the dom (even for elements outside grid!) when used in codesandbox. I understand that codesandbox is using real browser dom instead of jsdom.

I managed to make it work on my local machine, but I had to switch to jsdom 14 (check https://github.com/ianschmitz/jest-environment-jsdom-fourteen).

I've also needed to add this code in my tests setup (ag-grid is using innerText instead of textContent, without it I had no text inside rendered cells)

Object.defineProperty(global.window.Element.prototype, 'innerText', {
  set(value) {
    this.textContent = value;
  },
  configurable: true
})
like image 147
Michał Avatar answered Oct 28 '25 03:10

Michał