Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Migrate tests from enzyme to react-testing-library #22911

Closed
46 tasks done
oliviertassinari opened this issue Oct 6, 2020 · 31 comments · Fixed by #26832
Closed
46 tasks done

[test] Migrate tests from enzyme to react-testing-library #22911

oliviertassinari opened this issue Oct 6, 2020 · 31 comments · Fixed by #26832
Labels
core Infrastructure work going on behind the scenes test

Comments

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 6, 2020

15 months ago, we have introduced react-testing-library in the codebase: #15732. Since, then, we have been progressively migrating the tests from enzyme to react-testing-library. This was done as a background task, so far. Especially when we were migrating a component from class names to hooks or when we needed to fix a bug.

However, we have seen community members interested in helping with this effort, e.g. @baterson in #22441, @marcosvega91 in #20914, @emilyuhde in #17942, or @eladmotola in #22906.

I have opened this issue so we can keep track of the tests that are left to be migrated and avoid conflicts:

Integration

@Avi98
Copy link
Contributor

Avi98 commented Oct 6, 2020

@oliviertassinari can I pick some of the tests and migrate it react-testing-library

@JorgeAndd
Copy link

@oliviertassinari Can I give it a shot at migrating any of these files?

@itamar244
Copy link
Contributor

itamar244 commented Oct 8, 2020

Need help in testing Modal.test.js.

The following test depends on querying the Fade component and checking if it has specific prop.
I searched for a different approach of testing this with react-testing-library in the project and in the internet and couldn't find a fitting solution.

@GiselaMD
Copy link

GiselaMD commented Oct 8, 2020

@itamar244 I'm also trying to find a solution for that, the other tests have the same "find component" query

bewong89 pushed a commit to bewong89/material-ui that referenced this issue Oct 8, 2020
Introduce a way for describeConformance to use react-teating-library and convert accordion.test.js to use react-testing-library
bewong89 pushed a commit to bewong89/material-ui that referenced this issue Oct 8, 2020
Introduce a way for describeConformance to use react-teating-library and convert accordion.test.js to use react-testing-library
@NMinhNguyen
Copy link
Contributor

NMinhNguyen commented Oct 9, 2020

@oliviertassinari how do you want to handle tests that use describeConformance? Should those particular tests just be left as is?

@oliviertassinari
Copy link
Member Author

@NMinhNguyen Best to ask @eps1lon. I have ported a few of them to rtl on the data grid, but that won't cut it.

@bewong89
Copy link
Contributor

bewong89 commented Oct 9, 2020

🤔 I was wondering this myself, I took a stab at forking describeConformance to use react-testing-library as I thought the abstraction it provides was still useful 1b43a2c#diff-af11c81585e9558fff1a83088a7d2580

That basically makes it opt-in per component.

If this is an approach we want I'm happy to clean that code up and push through a seperate PR for describeConformance.

The other option we have is to duplicate those tests across components.

@oliviertassinari
Copy link
Member Author

@nicholas-l Yes, thanks for raising it. I have added them to the list. I think that we should move them to the corresponding components.

@nicholas-l
Copy link
Contributor

@oliviertassinari Do you mean into the same file as the unit tests or move the file to .int.js (or *.int.test.js) in the component folder?

@oliviertassinari
Copy link
Member Author

@nicholas-l Well, forget about it, let's solve one problem at the time.

@deiga

This comment has been minimized.

@deiga
Copy link
Contributor

deiga commented Oct 17, 2020

describeConformance needs enzyme because we explicitly implement "props inheritance". You can't test that with testing-library. No harm done using enzyme there.

Is it really necessary to test "props inheritance"? Isn't that kind of implied by using React? Or am I missing some point here?

@oliviertassinari
Copy link
Member Author

@deiga Yes, it's necessary, a component can break the inheritance.

@eps1lon
Copy link
Member

eps1lon commented Oct 17, 2020

Is it really necessary to test "props inheritance"? Isn't that kind of implied by using React?

I don't think that's a useful discussion to have. You can always argue like "isn't that implied by JS". If we would rely on "implied by React" then we'd actually use enzyme and not testing-library.

Or am I missing some point here?

We use tests to generate documentation. We actually solve two problems at once: maintaining documentation and testing implementation. While outdated documentation isn't that bad of a problem to have it's always annoying to deal with (outdated docs being around, issue being reported, someone has to fix it etc).

I don't understand why it's so important to you that we ban enzyme completely from our code base? It still has some value for certain edge cases especially for component libraries.

The main problem with enzyme is it's large API surface that is easy to abuse. The goal is to have a useful test suite where it's hard to write bad tests. Once every test suite is ported to testing-library we can hide enzyme entirely inside describeConformance or other dedicated helpers where writing bad tests is harder.

@deiga
Copy link
Contributor

deiga commented Oct 22, 2020

Sure, that makes sense 👍

I usually like to push extra hard to get rid of "too many" dependencies, and wanted to prod the reasoning behind keeping enzyme :)

@vividh
Copy link
Contributor

vividh commented Oct 25, 2020

May I claim migrating one of the remaining components, say Menu.test.js?

@willRicard
Copy link

Same here, I would like to migrate Popover.test.js

@oliviertassinari
Copy link
Member Author

Definitely, you can go ahead :)

@samrae7
Copy link

samrae7 commented Oct 25, 2020

@Avi98 are you still doingDrawer.test.js? If not I will

@vividh
Copy link
Contributor

vividh commented Oct 25, 2020

I'm having a harder time with migrating Menu than I expected. Could I get some help with the following issue?

Migrated the following unit test, but container.firstChild is null.

describe('prop: PopoverClasses', () => {
  it('should be able to change the Popover style', () => {
    const { container } = render(<Menu {...defaultProps} PopoverClasses={{ paper: 'bar' }} />);
    expect(container.firstChild).to.have.class('bar');
  });
});
1) <Menu />
       prop: PopoverClasses
         should be able to change the Popover style:
     TypeError: Cannot read property 'classList' of null
      at Proxy.<anonymous> (node_modules/chai-dom/chai-dom.js:80:10)
      at Proxy.methodWrapper (node_modules/chai/lib/chai/utils/addMethod.js:57:25)
      at Context.<anonymous> (packages/material-ui/src/Menu/Menu.test.js:105:7)
      at processImmediate (node:internal/timers:462:21)

@anasufana
Copy link
Contributor

Would this be an appropriate solution for testing the Modal Backdrop Fade transition with testing-library? As other people have suggested, I'm not sure there is a more elegant way of doing it with testing-library.

 const fadeTransition = (transition = 225) =>
      `opacity ${transition}ms cubic-bezier(0.4, 0, 0.2, 1) 0ms`;
it('should render a backdrop with a fade transition', () => {
      ...
      expect(backdrop.style).to.have.property('transition', fadeTransition());
    });
it('should pass prop to the transition component', () => {
     ...
      expect(backdrop.style).to.have.property('transition', fadeTransition(200));
    });

@oliviertassinari oliviertassinari removed the hacktoberfest https://hacktoberfest.digitalocean.com/ label Nov 1, 2020
@samrae7
Copy link

samrae7 commented Nov 1, 2020

Update on drawer.test.js - because this component uses the modal component it is hard to test with react-testing library and so I haven't proceeded. I'm guessing this is the reason it was left until now

@eps1lon
Copy link
Member

eps1lon commented Jun 19, 2021

Still have tests using createMount

@eps1lon
Copy link
Member

eps1lon commented Jun 30, 2021

The remaining tests either need enzyme or are testing legacy code.

Thanks everybody for your help. This will help alot during the React 18 pre-release.

@eps1lon eps1lon closed this as completed Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes test
Projects
None yet
Development

Successfully merging a pull request may close this issue.