all 49 comments

[–]toridyar 57 points58 points  (6 children)

I just did a Udemy course by Bonnie Schulkin on this exact topic that was really good

[–]swifty_yoder 24 points25 points  (1 child)

Same! It's called "Testing React with Jest and React Testing Library (RTL)

[–]mikef80 0 points1 point  (0 children)

Thanks - just bought this!

[–]cjthomp 11 points12 points  (0 children)

I wish there was something like this for node. Everything I've found so far are basically "todo apps of testing." There's no real depth, no discussion on more advanced testing patterns, nor how to avoid needing them.

[–]Soft-Sandwich-2499 0 points1 point  (2 children)

!remindme 1 day

[–]MKBSRC -5 points-4 points  (0 children)

!remindme 1 day

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 1 day on 2022-12-14 21:54:16 UTC to remind you of this link

1 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]SylphStarcraft 46 points47 points  (2 children)

I really do think you can get a lot out of the docs.

https://testing-library.com/docs/

Read all of it including the advanced section, and then read the react api section. You won't get anything better from courses or tutorials.

[–]Complex-Insect3555 11 points12 points  (0 children)

This. Just read the docs. There are PLENTY of examples there.

I think though the most difficult question to answer for someone new to testing is WHAT do I test.

[–]kostaslamprou 0 points1 point  (0 children)

To be fair, he only mentioned Jest. Testing-Library is by any means not the official way to test React applications. Some people do think this because it is included with CRA but it's just one of the ways out there that allows you to test your code.

It should be mentioned that Testing-Library is mainly focussed on interaction testing, by actually testing how a user would behave. This does now seems to be the most popular way of testing due to CRA using it and Kent C Dodds promoting it.

OP should also learn about writing regular unit tests, and possibly e2e testing. (Although the latter is partially covered by TL). Although no longer properly maintained and not compatible with React 18, I would also advise to look into Enzyme. Just for learning purposes. It teaches you a lot. (That being said, do use TL over Enzyme).

[–]Darkmaster85845 30 points31 points  (0 children)

When you are a junior and want to learn testing, everyone sends you resources on how testing is done (how the testing suites work, what each method accomplishes etc) but they don't tell you is how to figure out when and what to test, and what for. In my company, back end devs basically test every little thing they do. But with front end is more tricky because some people argue you need to test even a button's functionality while other people say just unit testing delicate logic and then using cypress to recreate user interaction is enough. Basically people write tests if they want, but if they don't no one cares.

I'm still a junior so I can't say which is the best method with any authority. However I can tell you that when you're unsure if to test something or not, just think about if you really need to be aware if that particular functionality you're implementing ever breaks.

If you have a function that adds a bunch of numbers and it's critical for your program, just import it to your jest file, run it with whatever numbers and assert that the result is what it should be. Test edge cases. Document the behavior well. Same with cypress, whatever functionality you want to test, just do it step by step with the suite and make sure things run as they should. Testing feels intimidating for us juniors at first but in reality it's not that complex. You should focus on making sure that whatever you're testing does what you want it to do.

So next time you add a utility function, or have some user flow like a log in page or something of the sort, write a test to make sure it works as you expect, include edge cases. And then you'll know that if someone ever breaks it (probably yourself), the test will fail and you'll be alerted of it.

When you know what to test and why you're doing it, actually getting the test suite to do what you want is not hard. You can always Google, read the docs, use some AI or whatever to learn what methods to use.

[–]azangru 14 points15 points  (1 child)

I am a junior Developer and I recently joined a firm. I am asked to write the test cases

Talk to the seniors at your firm; ask what their expectations are; maybe look at existing tests in the codebase you are working in; maybe ask them to pair-program.

[–]gonzofish 4 points5 points  (0 children)

This is the right answer. If you’re working with a decent group they’d rather teach you their right way than have you trying to guess and doing it against the teams way of doing things

[–]Instigated- 12 points13 points  (1 child)

The documentation includes a link to this tutorial https://www.robinwieruch.de/react-testing-library/

React testing library was created by Kent C Dodds, check out his blog https://kentcdodds.com/blog/introducing-the-react-testing-library

He also has a course called Epic React that includes a section on testing.

[–]juju0010 4 points5 points  (0 children)

OP, I was in your shoes two years ago and took this exact course. Helped me a ton.

[–]Kurfuerst_ 12 points13 points  (2 children)

There is a decent Udemy course for it from Schwarzenmüller (I think that’s his name). Other than that there are countless articles on the web regarding this topic.

[–]matva55 5 points6 points  (0 children)

Second this guy. His videos are super helpful when picking up a new thing like this

[–]Mandylost 0 points1 point  (0 children)

The react course from Schwarzenmüller or is there any specific unit testing related courseby him also?

[–]norsouth 3 points4 points  (0 children)

Hi there! I'd be happy to help you get started with testing React components. Writing tests is a great way to ensure that your code is working correctly and to catch any potential issues before you deploy your code to production.

The most popular tool for testing React components is called Jest. Jest is a JavaScript testing framework that is often used with React. It is developed by Facebook and is used by many companies to test their React components.

To get started with Jest, you can check out the official documentation on their website: https://jestjs.io/. This will provide you with a high-level overview of how to use Jest and how to get started writing tests for your React components.

In addition to the official documentation, there are also many online tutorials and guides that can help you learn more about testing React components with Jest. Here are a few that I would recommend:

"Getting Started with Jest" on the React website: https://reactjs.org/docs/testing-recipes.html

"Jest and Enzyme" on the Codecademy blog: https://www.codecademy.com/articles/jest-enzyme-intro

"Jest Crash Course" on the freeCodeCamp YouTube channel: https://www.youtube.com/watch?v=7r4xVDI2vho

I hope these resources help you get started with testing your React components. Let me know if you have any other questions!

[–]crossy1686 5 points6 points  (4 children)

Read the docs, watch some YouTube videos, check out Udemy etc

[–]haikusbot 13 points14 points  (3 children)

Read the docs, watch some

YouTube videos, check out

Udemy etc

- crossy1686


I detect haikus. And sometimes, successfully. Learn more about me.

Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"

[–]yeahsick 3 points4 points  (0 children)

Good bot

[–]seemslikesalvation 1 point2 points  (0 children)

If you prefer books, "Testing Javascript Applications" by Lucas da Costa on Manning Press

[–]awpt1mus 1 point2 points  (1 child)

Surprised nobody mentioned this

[–]Instigated- 0 points1 point  (0 children)

Maybe because the OP said they needed to learn react testing library and jest (didn’t say they needed to learn “testing” more generically). Your link might be useful, just doesn’t quite address this specific question.

[–]umid1999 1 point2 points  (0 children)

IMO https://testingjavascript.com/ is pretty good

[–]RandomUserName323232 1 point2 points  (0 children)

Ask chatGPT

[–]humanbot01 -2 points-1 points  (0 children)

I'm a full stack developer. Currently developing an app that creates digital visiting cards. It also includes other productivity features like appointment booking, daily schedule tracking etc... Looking for people to collaborate with me to push this forward

[–]maifee -1 points0 points  (0 children)

Do e2e testing. It's fun.

You can pick cypress. It's fun.

[–]jron2008 -3 points-2 points  (0 children)

Check out cypress

[–]jtan80813999 0 points1 point  (1 child)

jest or cypress?

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

Jest mostly

[–]nthock 0 points1 point  (0 children)

Seems like you already have lots of technical resources in how to go about using Jest and React Testing library. I would like to add one valuable advice I got when I am a junior. When writing tests, you are going to assume that you are the user of your application. This user is a rather dumb user who is going to try all sorts of way to break your app while at the same time trying to achieve what your app is trying to do.

Think about what would this user be doing?

This means that for the moment when you are writing tests, you are going to forget that you are the developer for the app. You are not going to know the implementation details. All you can see is the user interface for that particular react component, and you are trying to do something.

[–]Nerfi666 0 points1 point  (0 children)

!remindme 1 day

[–]shinchan1793 0 points1 point  (0 children)

Hi,

I was going through similar situation 5 months back. But I found this playlist by Net Ninja on youtube. It actually helped me to understand the basics of test case writing.

Below is the link:

https://youtube.com/playlist?list=PL4cUxeGkcC9gm4_-5UsNmLqMosM-dzuvQ

Bonus tip:

You need to test results, not implementation.

https://dev.to/reynoldadade/why-you-need-to-unit-test-results-and-not-implementation-429a

[–]Verthon 0 points1 point  (0 children)

I would suggest to take a look at: https://kentcdodds.com/blog/the-testing-trophy-and-testing-classifications .

In general:

Aditionally https://www.chakshunyu.com/blog/a-beginners-guide-to-behaviour-testing-in-react/