mui-public icon indicating copy to clipboard operation
mui-public copied to clipboard

[code-infra] Add react-testing-library eslint plugin

Open oliviertassinari opened this issue 1 year ago • 3 comments

configure eslint-plugin-testing-library and fix issues

See https://github.com/mui/mui-toolpad/pull/3648 as an implementation example.

Could also be helpful in:

  • [ ] 1. #174
  • [ ] 2. Use screen at the import level, not the mounting point level:
-import { render } from '@testing-library/react';
+import { render, screen } from '@testing-library/react';

   test('renders content correctly', async () => {
-    const { getByText } = render(<AppProvider>Hello world</AppProvider>);
+    render(<AppProvider>Hello world</AppProvider>);

-    expect(getByText('Hello world')).toBeTruthy();
+    expect(screen.getByText('Hello world')).toBeTruthy();
   });
 });

For example https://github.com/mui/material-ui/pull/41061#discussion_r1632481628

  • [ ] 3. Use user at the import level, not the mounting point level: https://github.com/mui/mui-x/pull/14142#discussion_r1739138251. Actually maybe not: https://github.com/mui/material-ui/pull/43804#discussion_r1765458889.

oliviertassinari avatar Jul 12 '24 19:07 oliviertassinari

Now that we're experimenting with vitest, it looks like using global screen (and global cleanup) prevents our tests from running in parallel. We should actually do the opposite of their recommendation if we want to strictly isolate our tests 😄

Janpot avatar Oct 10 '24 16:10 Janpot

Are they mounting multiple components on the same jsdom instance in the same JavaScript context? I would expect that Vitest uses multiple node processes, and hence it doesn't matter. Or can we assign the right jsdom instance to the screen API with a mutation?

oliviertassinari avatar Oct 10 '24 17:10 oliviertassinari

Are they mounting multiple components on the same jsdom instance in the same JavaScript context? I would expect that Vitest uses multiple node processes, and hence it doesn't matter. Or can we assign the right jsdom instance to the screen API with a mutation?

Vitest uses multiple processes, that works, but seems to be about 3x slower than mocha (will have to investigate further). I tried disabling their isolation but it acts too weird, even when disabling the per-file parallelism. Probably a dead end for now, yes...

Janpot avatar Oct 11 '24 08:10 Janpot

Everything is prepared from infra side, we need to enable it per repository and fix all the issues that will appear.

mnajdova avatar Oct 03 '25 12:10 mnajdova