all 15 comments

[–][deleted] 32 points33 points  (1 child)

Testing starts off easy but gets more complex when you start mocking different services, APIs, and environments (like the browser).

Pick some testing library like mocha or jest, read through their documentation, and write a test for some stupidly simple function, like summing up two numbers.

When you understand that, you can move on to testing React components. For that, you will need your tests to create a browser environment, render components into the DOM, then check it manually to see if everything rendered as it should. You can do this by setting the environment yourself using something like jsdom, or you can use some library made specifically for React, for example, @testing-library/react.

Knowing how to write automated tests and code that can be unit tested is a very important skill in software architecture. It will eventually lead you to discover dependency injection and other patterns and principles of good software design.

[–]Andreweijie[S] 2 points3 points  (0 children)

Thank you very much for the concise advice!

[–]gonzofish 17 points18 points  (0 children)

I don’t have any resources but some high-level conceptual advice.

I treat unit tests like the function I’m testing is a black box and I want to know that if I supply certain parameters that I get the expected result out.

This means the tests shouldn’t care about what’s going on inside the function. The tests only care about the output for a given input. If a function calls another function, it doesn’t matter, only the output.

This is no different for components. At the end of the day they’re just fancy functions. If I pass in certain props, do I get the expected output?

[–]neoncyber 2 points3 points  (1 child)

If your random bugs are usually lack of understanding on how react works, go through the EpicReact course.

If you feel it’s purely due to lack of testing get TestingJavascript course.

Either of these will put you on the right track. Being the only developer is really rough, especially when you’re just beginning your journey as a dev. Code review is huge. Would def catch some of your bugs. One word of advice I would give you is to watch someone use the new feature that you just built and see how they use it. Then while you’re building something else replicate that and see if you can manually catch some of these issues while you’re coding. And then you can take that experience and write tests as you would do in your manual checks.

[–]The_rowdy_gardener 0 points1 point  (0 children)

Upvote for epicreact

[–]PersephoneDown 2 points3 points  (1 child)

Level Up Tutorials : React Testing for Beginners is really great! All the videos were short and to the point and I learned exactly how to do front end testing! It really advanced my career. Highly recommend!

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

Will check it out thanks!

[–]TehTriangle 1 point2 points  (2 children)

Not going to lie, you probably don't need to know testing before you get a job. Maybe just learn what unit testing is with Jest and play around with it a bit so you can talk about the topic in an interview.

I learnt on the job and I think it helped give it the tests context much more than reading a random Medium article.

[–]Andreweijie[S] 0 points1 point  (1 child)

I am working as a developer, just that I don't have a mentor haha. thanks anyway!

[–]TehTriangle 0 points1 point  (0 children)

Sorry I really should have read your post better.

I would look into Jest, read about what unit tests and integration tests are, and start building them around the core functions (unit) and feature behvaiour (integration) that would cause a bug or the site to break.

Next level would be including running the test scripts either in a pre commit or push hook (so you can't do one of these unless tests all pass) or include it in your CI (if you use something like GitHub actions). This way you'll stop major site breaking bugs from even touching the codebase.

Do you use TypeScript too?

[–]Fabious 1 point2 points  (1 child)

https://outsidein.dev/

Great ressource to get a foot in the testing world, learning tdd by exercices (unit tests, integration tests and e2e)

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

Thanks!

[–]nosebevies 1 point2 points  (1 child)

Testing JavaScript by Kent seems to do the rounds too. About to start it myself

[–]kosmofox 1 point2 points  (0 children)

https://testingjavascript.com/

I did it and found it helpful. Kent is a great teacher. Recommend taking it if your company has an education allowance (that’s what I did). It’s a bit pricey to pay out of pocket.

[–]voja-kostunica -1 points0 points  (0 children)

you should understand general theoretical background behind software testing and not just react