joshwcomeau / guppy

🐠A friendly application manager and task runner for React.js
ISC License
3.27k stars 154 forks source link

[WIP] Component unit tests using react-testing-library #339

Closed idoberko2 closed 5 years ago

idoberko2 commented 5 years ago

Related Issue:

309 - react-testing-library attempt

Summary: Examine the usage of react-testing-library to cover component testing

idoberko2 commented 5 years ago

@AWolf81, I started with tests for Sidebar. I'm not sure it's such a good case for examination since this component doesn't have much text in it, and react-text-library is very text-oriented (its powerful tools are getByText and getByLabelText by which you can query the DOM, similar to the way the user interacts with the UI). Please have a look when you have time and I will try to add tests for another component.

AWolf81 commented 5 years ago

@idoberko2 Thanks for adding the Sidebar test. I've created that test for enzyme as well. I'll open a PR in a minute so it's easier to compare.

It looks OK but I'm getting an error message in every test - not sure why. It looks like following:

TypeError: Cannot read property 'now' of undefined
      at Object.<anonymous> (node_modules/wait-for-expect/lib/index.js:16:21)
      at Object.<anonymous> (node_modules/dom-testing-library/dist/wait.js:10:45)
      at Object.<anonymous> (node_modules/dom-testing-library/dist/index.js:76:13)

Seems like a configuration issue but I couldn't find the cause yet.

melanieseltzer commented 5 years ago

I'm getting the Cannot read property 'now' of undefined errors as well. Can't seem to find anything concrete on Google. Maybe it's something to do with testEnvironment?

From skimming over the code it seems like react-testing-library is much less verbose than Enzyme which I quite like.

idoberko2 commented 5 years ago

@AWolf81, @melanieseltzer, so what do you say? Should we drop react-testing-lib and stick with Enzyme? Do you feel like it's worth another shot?

AWolf81 commented 5 years ago

@idoberko2 I think we should stick to Enzyme as there are more examples for tests, no issues in the setup and from the tests we've wrote there is no big difference between the two testing libs. So I think it's better to use the more mature library.

Some other opinions to Testing-lib vs enzyme can be found in this DEV discussion.

melanieseltzer commented 5 years ago

I'm going to agree and say let's stick with Enzyme for now :-)

AWolf81 commented 5 years ago

We can close this for now as the PR for Enzyme will be merged soon. Just waiting for the feedback from @idoberko2 to check if the Enzyme setup & the first tests are OK.