react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

Jest's afterEach(cleanup) when rendering svg's causes Error: Uncaught [TypeError: this.node.getScreenCTM is not a function]

Open rodpatulski opened this issue 3 years ago • 6 comments

Relevant code or config:

// in app.test.js
import '@testing-library/react/dont-cleanup-after-each';
afterEach(cleanup);

it('renders the app without errors', done => {

  render(<App />);
  
});

app.js renders an svg chart. (Apexcharts)

What you did:

Ran npm test

What happened:

Error: Uncaught [TypeError: this.node.getScreenCTM is not a function]

Problem description:

When I setup cleanup with jest either through its automatic feature or by turning it off and calling it manually in 'afterEach'.

afterEach(cleanup);

The code breaks.

If I use:

// in app.test.js
afterEach(() => {
  setTimeout(() => {
    cleanup(); 
  }, 1);
});

Things work fine.

Suggested solution:

For some reason, cleanup seems to be called prematurely (before all the unmounting events?). The solution needs to play well with jest's afterEach mechanism and cleanup.

rodpatulski avatar Aug 26 '22 23:08 rodpatulski

Thanks for opening this one @rodpatulski. If I understand correctly, we're not awaiting for the unmount, we're just calling root.unmount() so what you're experiencing seems reasonable. There's probably something happening on unmount of Apexcharts. From a first look at the repo, it looks like it's using class components so I highly doubt they support React 18.

MatanBobi avatar Aug 28 '22 06:08 MatanBobi

I highly doubt they support React 18.

I forgot to mention that this was happening to me as early as react 16. (I recently updated to 18)

rodpatulski avatar Aug 28 '22 17:08 rodpatulski

Seems like something is not cleaning up properly during unmount. Without knowing the full source, we won't be able to know where the issue is coming from. Try to remove as much code as possible until it no longer reproduces and share that either via codesandbox or a cloneable repository.

eps1lon avatar Aug 31 '22 06:08 eps1lon

Here's a repo that recreates the problem: Run npm test to recreate. https://github.com/rodpatulski/bug-jest-cleanup

rodpatulski avatar Sep 01 '22 00:09 rodpatulski

afterEach

@rodpatulski In the README you write

Uncomment afterEach(cleanup) line to see that the test passes without cleanup.

But I can't find that line. Are you sure you included all the relevant code?

eps1lon avatar Sep 04 '22 13:09 eps1lon

Sorry, forgot to push a commit. The 'afterEach(cleanup) is in app.test.js ln 7.

rodpatulski avatar Sep 20 '22 02:09 rodpatulski

Since this relies on a 3rd party library (react-apexcharts) I would suggest filing an issue against their repository first. Or try to reduce the repro to code that's not using 3rd party code.

Check out https://stackoverflow.com/help/minimal-reproducible-example for a comprehensive guide on creating reproductions.

eps1lon avatar Oct 08 '22 09:10 eps1lon

Closing since no minimal reproduction was provided within 14 days.

eps1lon avatar Dec 06 '22 20:12 eps1lon