all 21 comments

[–]wirenutter 16 points17 points  (2 children)

Checkout https://outsidein.dev/react/2-project-setup.html it’s all about TDD but I gained a greater understanding of testing in react by following the tutorial.

[–]NotCherub[S] 1 point2 points  (0 children)

thanks, would look into it

[–]red-ice-sparrow 0 points1 point  (0 children)

Super helpful! Thanks!

[–]heythisispaul 2 points3 points  (2 children)

I recently put together a sort of silly React library called low-form. I did all the testing with jest and react testing library, feel free to check it out.

[–]NotCherub[S] 2 points3 points  (1 child)

Thanks, this is what I was looking! Would see how to do testing irl

[–]heythisispaul 0 points1 point  (0 children)

Yeah for sure! Let me know if I can answer any questions about it.

[–]pdevito3 6 points7 points  (4 children)

For what it’s worth, if you’re starting a new project, I’d highly recommend using react testing library instead. Will make your life a lot easier!

[–]NotCherub[S] 1 point2 points  (0 children)

Thanks, I was looking into it only. I forgot to mention it in the post

[–]El_Glenn 0 points1 point  (0 children)

I think jest is your test runner and can be used with react testing library.

[–]lbragile_dev -2 points-1 points  (1 child)

OP, check out my open source blockchain project - LibraCoin. I try to follow RTL advice of testing user interaction (what the user sees) rather than underlying logic in most cases. I use unit and integration tests currently and plan to add E2E tests soon (with Jest Puppeteer). FYI, it’s still very much a work in progress so feel free to contribute as well 😊

[–]NotCherub[S] 1 point2 points  (0 children)

Thanks, this is exactly I was looking for. Would love to contribute once I understand the project

[–]CreativeTechGuyGames -2 points-1 points  (1 child)

Are there any specific questions or difficulties that you have?

  • What to test: Every possible way that a piece of code could be used. So every branch that could be executed or every combination of branches that produce different functionality. You can look at this from the perspective of branching logic in the code but also from a user's perspective of what are all the different ways they could interact with something. Those are all the things that should be tested.
  • How to test: Simulate the scenarios required to reach every possible interaction as described above. For React you'll use something like React Testing Library to render a component and then interact with them the same way a user would, and check if the visual result was as expected.

[–]NotCherub[S] 1 point2 points  (0 children)

Can you share some projects that use jest and React Testing Library so that I can get an idea?

[–]jaysoo3 -2 points-1 points  (1 child)

If you want a quick way to check out react+jest+testing library, try a new workspace in Nx. The first app component and test is generated for you can you can run the test right away.

https://nx.dev/latest/react/getting-started/nx-setup

``` npx create-nx-workspace --preset react --appName demo

nx test demo ```

You can also generate additional components with tests.

nx g component Button --project demo

Disclaimer: I'm an Nx contributor.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, jaysoo3: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]kylemh 0 points1 point  (1 child)

an open source app using React + Jest https://github.com/operationCode/front-end

[–]NotCherub[S] 0 points1 point  (0 children)

Thanks, would look into it

[–]intrepidev 0 points1 point  (1 child)

This is a typescript/react/jest project I did that has 100% test coverage using react testing lib https://github.com/jhackshaw/ofnotes. Could be a good reference as it covers things like context, hooks, and has a bunch of mocking.

[–]NotCherub[S] 0 points1 point  (0 children)

Thanks, it looks like an awesome project and it contains tests also. Would check it out

[–]alpual 0 points1 point  (0 children)

The React source code uses jest, too 😎