How I like to test my react components

  Рет қаралды 26,332

Web Dev Cody

Web Dev Cody

Күн бұрын

Just talking about RTL react testing library and some ways to test hooks
My VSCode Extensions:
theme: material community high contrast
fonts: Menlo, Monaco, 'Courier New', monospace
errors: Error Lens
extra git help: Git Lens
tailwind css intellisense
indent rainbow
material icon theme
prettier & eslint
ES7+ React Snippets
------------
🤑 Patreon / webdevjunkie
🔔 Newsletter eepurl.com/hnderP
💬 Discord / discord
📁. GitHub github.com/cod...

Пікірлер: 50
@EmreDev7-qk7sf
@EmreDev7-qk7sf 6 ай бұрын
we use translation keys for picking an element in test file. generally translation keys wont be changed, but actual words may change. but it wont break tests because t('some.translation.key') will always same in virtual rendering.
@SumanNath25
@SumanNath25 11 ай бұрын
The video sucks? No way bro. One of best I have watched. Very real and clear to learn.
@bugs389
@bugs389 2 жыл бұрын
Typescript was complaining about the value attribute because the type HTMLElement was not narrowed down to HTMLInputElement.
@WebDevCody
@WebDevCody 2 жыл бұрын
Nice thank you, so maybe I just need to cast it then?
@bugs389
@bugs389 2 жыл бұрын
@@WebDevCody Yup exactly
@Will4_U
@Will4_U 2 жыл бұрын
@@WebDevCody or you can create a function that will assert that the element is HTMLInputElement I do it, so I'm confident that element is a specific type.
@jlndev1017
@jlndev1017 2 жыл бұрын
@@Will4_U How would you do that assertion?
@naterpotatoers
@naterpotatoers Жыл бұрын
⁠​⁠@@jlndev1017just throw an “as HtmlInputElement” at the end of the statement
@devinosborne3396
@devinosborne3396 2 жыл бұрын
Honestly I agree man. I kinda like the approach you have here. The MVC stuff was a little harder to understand, but I feel like this video made more sense!
@WebDevCody
@WebDevCody 2 жыл бұрын
I’m still working on this MVC approach, but yeah I’m glad you see kind of where I’m coming from
@mokoboko2482
@mokoboko2482 5 ай бұрын
best case scenario i search for a topic and web dev cody alrdy made a vid abt it
@bugs389
@bugs389 2 жыл бұрын
Great video, I definitely agree with your stance on testing the state logic in isolation. Testing complex components via DOM can get really tedious for little value in my opinion.
@elProdigio
@elProdigio Ай бұрын
Nice! I think that the first aproach (component testing) is nice and you're indrectly testing all the internal logic. It's nice because you can change the implementation and with the second aproach (testing the custom hook) you're testing internal implementation details. But, if this custom hook is reusable, this need to be tested in isolation from whatever the component uses. BTW, Could you upload a TDD real world example? I see a lot of "calculator" example, which is fine, but not a real world (and more complex) example. Thanks!
@JSBroadcast
@JSBroadcast 2 жыл бұрын
I think, and forgive me if I'm wrong, that you slightly missed the point of integration/unit tests. At one point near the end of the video, you are saying "What if you change an aria label, or something, etc". That is one of the main points of testing. When you change something, your test is supposed to fail, and if it does, it's on you to decide if that was suppose to happen, and you have to update your test, or if it was an accident, and you revert your code. That is the primary purpose of testing. So if at one point in time, you change your tag's attribute, or you change anything in relation to that, you absolutely want your test to fail. If it doesn't, then your test is useless. In simple words, you shouldn't be writing tests as you are writing your code. Tests exist so they can notify you that something in your code has changed, and at that point, you have to decide if that change was intentional, aka, update the test to reflect that, or if its a regression, aka, fix or revert the code. Cheers
@WebDevCody
@WebDevCody 2 жыл бұрын
Yeah, but knowing if a labeled changed isn’t a valuable test. We mainly care that a button clicked in the UI properly does the things it should in our system. Having integration tests to verify text or labels in our front end is a waste of time imo
@jedliczosnek
@jedliczosnek Жыл бұрын
@@WebDevCody True but at the same time we should also verify if the correct aria labels and other attributes are set on the rendered elements so I wouldn't disregard the component DOM testing completely.
@shawn.builds
@shawn.builds 2 жыл бұрын
you seem to post vids on everything im curiuos about. cheer mate
@HaibertBuilds
@HaibertBuilds 11 ай бұрын
this was awesome, thanks!
@igboanugwocollins4452
@igboanugwocollins4452 2 жыл бұрын
Thank you for this
@dangthien158
@dangthien158 2 жыл бұрын
Thank you for your sharing. Could you make a video about setup/config mock data testing with firebase?
@qualinostJT
@qualinostJT 2 жыл бұрын
Hi, thanks for your videos, they all are really informative. Do you mind to share this little project to see how you wrote your hook ?
@WebDevCody
@WebDevCody 2 жыл бұрын
It’s in a repo called react-testing on my GitHub repo list
@qualinostJT
@qualinostJT 2 жыл бұрын
@@WebDevCody Thanks a lot!
@reinyel1437
@reinyel1437 Жыл бұрын
This is exactly my thought on Testing. It doesn't have to be everything, just the most important part. Extracting the logic out of the component is more sensible to me rather than testing the whole component which is tedious, brittle, and hard to read.
@WoozyDev
@WoozyDev 2 жыл бұрын
9:50 because its a HTMLElement, u should do "input instanceof HTMLInputElement".
@WoozyDev
@WoozyDev 2 жыл бұрын
or "(await renderedApp.getByLabelText("todo-text")) as HTMLInputElement"
@WebDevCody
@WebDevCody 2 жыл бұрын
Thank you! I’ll try to remember that
@rjmunt
@rjmunt 2 жыл бұрын
Nice videos. I get the feeling that you're kinda abusing aria labels, just because you like to use them as test selectors ?
@WebDevCody
@WebDevCody 2 жыл бұрын
Yes I probably did in this video
@butwhothehellknows
@butwhothehellknows 2 жыл бұрын
Good job babe!
@nawti2371
@nawti2371 2 жыл бұрын
Hey man, I love your react videos they're really helpful ❤. Can you maybe do a video about Vite or Webpack configs that you use? I'm looking for some helpful configs that I can use for production which reduce the bundle size and optimizes performance for my react app. I've used some configs for production before but I wasn't using any frameworks for those projects and they were just native Js applications but somehow I'm having difficulties finding the right configs for my react applications. Thank you
@xinzhang7278
@xinzhang7278 2 жыл бұрын
can we make a video for Node Express API unit test ?
@27sosite73
@27sosite73 Жыл бұрын
thank you
@ajithmoni1712
@ajithmoni1712 2 жыл бұрын
Thank you. I know about testing-library now. I'll read it over. I've seen people use mocha and selenium JavaScript. Is this library built only for react and more preferred over selenium?
@WebDevCody
@WebDevCody 2 жыл бұрын
Selenium is more for e2e testing where You actually test in different browsers, RTL is for component and integration testing.
@shedrachelurihu4883
@shedrachelurihu4883 Жыл бұрын
I normally pass data-testid as a props to my component.
@jdavis2581
@jdavis2581 2 жыл бұрын
There’s about as much test code as there is production code…is this typical for more complicated applications too?
@WebDevCody
@WebDevCody 2 жыл бұрын
Yeah, it depends on how important the feature is or your teams thoughts on testing.
@TheProcessor
@TheProcessor 2 жыл бұрын
getByRole is the top priority to resemble a user
@WebDevCody
@WebDevCody 2 жыл бұрын
Yeah for sure, I do wonder why since checking by role and text content sounds like a brittle way to test. If someone changes the text of a button all your tests break? If you read the cypress guide they recommend using data-cy attributes to test with to allow non brittle tests
@TheProcessor
@TheProcessor 2 жыл бұрын
@@WebDevCody some would argue that’s exact what you want. Your tests are written so they break when something changes. Could help prevent typos or something. A text content change seems like a low effort change to a unit test anyway.
@WebDevCody
@WebDevCody 2 жыл бұрын
@@TheProcessor yeah, sure it’s low effort. I guess I’ll reconsider the idea
@mahdimurshed3679
@mahdimurshed3679 Жыл бұрын
What is the extension you are using for showing inline ts errors?
@WebDevCody
@WebDevCody Жыл бұрын
error lens
@manindra502
@manindra502 2 жыл бұрын
What's your Color theme & font name ?
@kittysplode
@kittysplode Жыл бұрын
wow, so you literally have no idea what you're doing. making major changes while explaining something, waste of data
@WebDevCody
@WebDevCody Жыл бұрын
I never know what I'm doing
@BLOBBERNATOR
@BLOBBERNATOR Жыл бұрын
Honestly, it's extremely helpful just seeing another devs toolset and see their thought/work process. Really helps with broadening out knowledge and especially helps with your problem-solving skills to frontend problems. Thanks for the vid Cody,
@Dlntck
@Dlntck Жыл бұрын
He obviously does. He said he didn't prepare for the video. Doesn't mean he doesn't know what he's doing.
Testing In React Tutorial - Jest and React Testing Library
21:28
Single Responsibility Principle in React (Design Patterns)
16:50
Cosden Solutions
Рет қаралды 52 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 500 М.
Test-Driven Development // Fun TDD Introduction with JavaScript
12:55
You might not need useEffect() ...
21:45
Academind
Рет қаралды 179 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 506 М.
React Testing Tutorial with React Testing Library and Jest
41:43
Un-Suck Your React Components - Composable & Compound Components
15:47
The important things to know about React state and renders
18:33
Web Dev Cody
Рет қаралды 28 М.
All useEffect Mistakes Every Junior React Developer Makes
22:23
If you don't write tests, you're NGMI
10:43
Web Dev Cody
Рет қаралды 10 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН