React Testing Tutorial (Jest + React Testing Library)

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.พ. 2021
  • Learn how to write unit tests for your React components using Jest and the React Testing Library.
    We will be writing some unit tests for React components using the Jest framework (jestjs.io/). We will begin by creating a react app using create-react-app. Then we will create a component & write some unit tests for it. We will finish by writing some snapshot tests to verify changes to the component tree.
    Don't forget to Subscribe here: / @krisfoster1
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 130

  • @skyhawksp
    @skyhawksp 3 ปีที่แล้ว +7

    The most useful react jest testing tutorial video currently on TH-cam. Thank you!

  • @marciocamollez7062
    @marciocamollez7062 ปีที่แล้ว +32

    Simply the best explanation from entire TH-cam. I'm studying react from 2 years and test library was the worst part to find some good videos. Congrats!

  • @andersonsimeon4172
    @andersonsimeon4172 3 หลายเดือนก่อน +1

    For a 3 years ago video, this is high quality staright to the point content. Thanks Man

  • @peacekper
    @peacekper 7 หลายเดือนก่อน +1

    Thank you so much for this tutorial! I would agree with the other comments too about seeing a more involved testing suite using a state library. But this was by far the best video I've seen on using jest. Keep the videos coming

  • @JuaNeMe
    @JuaNeMe 3 ปีที่แล้ว +6

    Excellent video! I have been waiting a video like this with React Testing in an useful way for a long time.

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +2

      That's great, thank you for the feedback @juan ocho!

  • @christonit6895
    @christonit6895 2 ปีที่แล้ว +6

    Uff thanks man! I have been all day reading the jest and react docs and wasn't understanding how to use these techs XD you just teched me how to properly unit test my UI lib.

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว

      Glad you enjoyed the video!

  • @rupeshmody8901
    @rupeshmody8901 ปีที่แล้ว

    Very simple and so far the best. Thank you.

  • @chenyangwang1930
    @chenyangwang1930 ปีที่แล้ว +1

    Perfect tutorial! Really easy to understand

  • @jackstorm777
    @jackstorm777 ปีที่แล้ว

    Excellent tutorial, lots of good info in 20 minutes!

  • @carlosg8716
    @carlosg8716 2 ปีที่แล้ว +3

    Excellent video. I went from zero to added to my projects in 22 minutes and 15 seconds

  • @Whiskey0
    @Whiskey0 ปีที่แล้ว +9

    Great tutorial mate, would love to see a more complicated test suite on a component that is using redux!

  • @SoftwareSimplified-Dtons
    @SoftwareSimplified-Dtons 9 หลายเดือนก่อน +6

    Thank you Kris for the wonderful learning videos . Just a small suggestion. The test was failing with '' so at 16:44 please change from expect(todoElement).toContainHTML(''); to expect(todoElement).toContainHTML('strike');

  • @gnumanoj
    @gnumanoj 2 ปีที่แล้ว +1

    most useful video so far ! :) Thank you!

  • @seriouslee4119
    @seriouslee4119 ปีที่แล้ว

    Bru, your voice is so soothing. I'm rewatching this video tonight instead of ASMR lol.

  • @joseff5998
    @joseff5998 ปีที่แล้ว

    Thanks for clear and simple explanation.

  • @mahmoudhamdy819
    @mahmoudhamdy819 6 หลายเดือนก่อน

    thank you so much for great and brief react test explanation

  • @gina7898
    @gina7898 ปีที่แล้ว

    This was so helpful. Thank you!

  • @EminoMeneko
    @EminoMeneko 2 ปีที่แล้ว +1

    Nice quick start.

  • @MrAndykofi
    @MrAndykofi 2 ปีที่แล้ว

    Excellent, thanks a lot, was good to watch.

  • @MietekPomywacz
    @MietekPomywacz ปีที่แล้ว

    Very good video, explained in a simple way :)

  • @aaronmatto1375
    @aaronmatto1375 2 ปีที่แล้ว

    Amazing video. Thank you!

  • @alainhonorekubwayo8819
    @alainhonorekubwayo8819 ปีที่แล้ว

    Excellent tutorial!

  • @josephcobbinah5412
    @josephcobbinah5412 ปีที่แล้ว +1

    Great stuff man!!

  • @harelweingarten
    @harelweingarten ปีที่แล้ว

    Great tutorial , TNX.

  • @spytyler
    @spytyler 3 ปีที่แล้ว

    Great video. Very helpful

  • @jaceborg
    @jaceborg 3 ปีที่แล้ว +1

    Great video, really well explained, thanks :)

  • @charithamila9194
    @charithamila9194 3 ปีที่แล้ว

    Thanks a lot. Very helpful

  • @khandoor7228
    @khandoor7228 3 ปีที่แล้ว +7

    please do more with testing!

  • @todaysrapsux123
    @todaysrapsux123 3 ปีที่แล้ว

    thank you! subscribed!

  • @sukhsingh7057
    @sukhsingh7057 ปีที่แล้ว

    Great guide!

  • @oliver1231033
    @oliver1231033 3 ปีที่แล้ว +1

    awesome content, thank you!

  • @marvinotieno2194
    @marvinotieno2194 4 หลายเดือนก่อน

    This video is really good

  • @user-gc5wf1rg6h
    @user-gc5wf1rg6h 8 หลายเดือนก่อน

    Thank you so much!

  • @cristoslaher
    @cristoslaher 2 ปีที่แล้ว

    Great! Really helpful

  •  3 หลายเดือนก่อน

    awesome content , thank you very much your time.

  • @dinidulochana3295
    @dinidulochana3295 2 ปีที่แล้ว +1

    Great content!!
    Thank you.

  • @mauroinde
    @mauroinde ปีที่แล้ว

    new subscriber, clear class

  • @marcelhumes1360
    @marcelhumes1360 ปีที่แล้ว

    Thank you Kris.

  • @bautista-cutraro
    @bautista-cutraro 3 ปีที่แล้ว

    Great video!

  • @Saje3l
    @Saje3l 2 ปีที่แล้ว

    Totally Awesome!!!

  • @martinharyanto4465
    @martinharyanto4465 ปีที่แล้ว +4

    Hi Kris ! thanks for explanation, I know this video is 2 years old, is it possible for you to re-explain it with current state of Jest + React Testing Library? Probably some improvement here and there? And maybe you could help to explain about testing user event + mocking child component inside the component being tested. Thanks !
    This is a very simple and great tutorial 👍

  • @arunimaroy4383
    @arunimaroy4383 ปีที่แล้ว +3

    toContainHtml method expects to pass html tag as a string without tag notaion so you need to replace '' with 'strike'.

  • @lyndontan1561
    @lyndontan1561 3 ปีที่แล้ว +3

    Perfect time and a perfect example thank you!!!! from PH
    please do more sample like using hooks State T.I.A

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +1

      Thank you for the feedback!

  • @554-manjushriparkhe4
    @554-manjushriparkhe4 2 ปีที่แล้ว +1

    Thank you so much for this vedio great vedio

  • @sureshanands
    @sureshanands 3 ปีที่แล้ว +2

    Bro can yu make videos for async methods RTL testing. Some other examples which helps real time project experience. This video is also good . If you make those tht would be best ❤️👍

  • @user-cs4nu4ch3l
    @user-cs4nu4ch3l 9 หลายเดือนก่อน

    Nice hi Kris,
    From Ireland too

  • @nusskugeldingsi
    @nusskugeldingsi 2 ปีที่แล้ว +5

    Nice overview of testing with Jest in React!
    BTW the Todo component inside the map loop is missing a key prop. ;)

    • @I3uzzzzzz
      @I3uzzzzzz ปีที่แล้ว

      ur life is missing a dad ;)

  • @smhmkkh
    @smhmkkh 2 ปีที่แล้ว

    Thank you, that was great. The only part i didn’t understand was what is snapshot

  • @ManishKumar-mi7ko
    @ManishKumar-mi7ko 3 ปีที่แล้ว

    thank you so much

  • @AhmadMaartmesrini
    @AhmadMaartmesrini 4 หลายเดือนก่อน

    thank you

  • @oleksandrhusiev6544
    @oleksandrhusiev6544 2 ปีที่แล้ว +1

    Top, thanks!

  • @adityanair1001
    @adityanair1001 2 ปีที่แล้ว

    Thanks bruh ❤️
    Can u make more of these vedios
    How how to test for the data in a component which we r getting from api.

  • @damo190
    @damo190 3 ปีที่แล้ว +1

    Thanks for this video bro. Will u start a video series on Reactjs testing just like using, mocha, chai, cypress, knightwatchJs, jasmine, karma, react testing library, etc?

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +3

      I made some videos a while back on some of those technologies:
      cypress - m.th-cam.com/video/woI490HRM34/w-d-xo.html
      mocha/chai - m.th-cam.com/video/FKOstRrwT5w/w-d-xo.html

  • @vadirajkanakagiri5028
    @vadirajkanakagiri5028 ปีที่แล้ว +2

    I'm getting the error as SyntaxError: Cannot use import statement outside a module, can you please help me with this

  • @bhaskardas3777
    @bhaskardas3777 ปีที่แล้ว

    U are awesome.

  • @himallimbu007
    @himallimbu007 2 ปีที่แล้ว +3

    toContainHTML function is not working properly, can you please help?

  • @cloudsss83
    @cloudsss83 11 หลายเดือนก่อน

    you should create more tuts... you`re good. thanks

  • @HeshamElQuerm
    @HeshamElQuerm 2 ปีที่แล้ว

    Good video, but why did you `mock` the props in the test file? Isn't there a more elegant way to get the real props that are passed to the component?

  • @aprimic
    @aprimic 2 ปีที่แล้ว

    in what circumstance will the snapshot not match?

  • @maxdev6347
    @maxdev6347 4 หลายเดือนก่อน

    Which node version you use

  • @awefawefwaef
    @awefawefwaef ปีที่แล้ว

    what window manager are you using?

  • @aanandantma9573
    @aanandantma9573 2 ปีที่แล้ว

    Are you using any extension for jest intellisense?

  • @BarbarosYurttagul
    @BarbarosYurttagul หลายเดือนก่อน

    Hi Kris, thanks for the video. 21:26 How did you manage to reduce the test execution speed from 2+ seconds to 0.666 seconds? I did the same steps but mine didn't change.

  • @lekkyjayz6294
    @lekkyjayz6294 3 ปีที่แล้ว +1

    Can you make a tutorial about react redux unit testing?

  • @patrickackom952
    @patrickackom952 ปีที่แล้ว

    how do you test apps with usecontext and api calls in them

  • @gmmkeshav
    @gmmkeshav ปีที่แล้ว

    what if we want to have more folders in __tests__ ?

  • @grumly85
    @grumly85 ปีที่แล้ว +1

    I've got pink panther in my head now with all these 'todo, todo, todo,...'
    Nice tutorial still

  • @coolpythoncodes5768
    @coolpythoncodes5768 2 ปีที่แล้ว +1

    🚀🚀🚀

  • @juhandvan
    @juhandvan 2 ปีที่แล้ว +1

    thanks

  • @saraswathijampala510
    @saraswathijampala510 ปีที่แล้ว

    HI.............the node version u used is 14 r 16........? Please share me i tried in both version 14 and 16 but its not working.

  • @jeff2000wu
    @jeff2000wu ปีที่แล้ว

    getting an error for this line: import renderer from 'react-test-renderer'; I install the package by npm install react-test-render , anyone know.

  • @danieltkach2330
    @danieltkach2330 ปีที่แล้ว

    I'm using Vite and not CRA, so the script for test does not exist, how do I set it up?

  • @ainneo
    @ainneo 2 ปีที่แล้ว

    it there a git repo for this?

  • @mybaby2262
    @mybaby2262 2 ปีที่แล้ว

    How to write a test cases for big component lets say I have component with big business logic how could I write test cases for that

  • @poenaeco
    @poenaeco 3 ปีที่แล้ว +2

    What is the extension you are using in the console? Thanks for the tutorial btw.

    • @poenaeco
      @poenaeco 3 ปีที่แล้ว +1

      I mean for the GIT.

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +1

      Hi @poenaeco, it's called ohmyzsh. I find it very useful. Here's a link with steps to install ohmyz.sh/

    • @poenaeco
      @poenaeco 3 ปีที่แล้ว +1

      @@KrisFoster1 Thanks for the reply.

  • @vin2629
    @vin2629 ปีที่แล้ว

    Need a more advanced tutorial where the API calls are mocked

  • @akunduru4
    @akunduru4 3 ปีที่แล้ว

    Thanks for the video. How do we get that back quote in the VSC in the data-testid={'todo-${id}'} ?

  • @Leo-bl3ge
    @Leo-bl3ge 3 ปีที่แล้ว +1

    Why does my simple tests take so long?
    A simple expect(true).toBe(true) takes(2ms) but Time was 3s (yours was 1s)
    Edit: I was able to make it 1.9s with --maxWorkers=50%, but idk if there's more to do

  • @ZawWLwin-jo7dc
    @ZawWLwin-jo7dc 2 ปีที่แล้ว +25

    The usual flow for TDD is to write the test first, watch it fail, and make a minimal effort to get it right and then refactor. But anyway, greate video.

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว +22

      Thank you for the feedback @Zaw W. Lwin, TDD is just one approach to take. This tutorial is not on TDD but just on how to test react components.

    • @137dylan
      @137dylan 2 ปีที่แล้ว +1

      @@KrisFoster1 Even Kent Dodds said he rarely goes full TDD and prefers to use a more iterative approach

    • @jackstorm777
      @jackstorm777 ปีที่แล้ว +1

      TDD is an unattainable ideal, like world peace or eternal youth, it's nice to think about but will never EVER happen :)

    • @timdanielewski8036
      @timdanielewski8036 ปีที่แล้ว

      ​@@jackstorm777oh it's real! Maybe they call it extreme programming because it's extremely rare. I stumbled into a team of 8 developers that pair program and swap partners everyday. Not one line of code was written with out a test existing first. One of the funnest things I've ever been a part of.

  • @paviyarasanv4192
    @paviyarasanv4192 ปีที่แล้ว

    Can you please upload. how to test redux login page

  • @deathmetal0073
    @deathmetal0073 ปีที่แล้ว

    Kewl

  • @jeanlaguna6949
    @jeanlaguna6949 ปีที่แล้ว +1

    nice tutorial. Just one thing.... toContainHTML receive just the name of the tag without the ""

    • @TlbHibiki
      @TlbHibiki ปีที่แล้ว

      I was just going through this tutorial and I had this issue with toContainHTML not liking and should be changed to remove the .

  • @lekkyjayz6294
    @lekkyjayz6294 3 ปีที่แล้ว +2

    Instead of:
    import renderer from 'react-test-renderer'
    use:
    import TestRenderer from 'react-test-renderer'

  • @Steel0079
    @Steel0079 2 ปีที่แล้ว

    Thanks for taking the time to make the video on the topic. I am running into a problem, near 17:07, where you place the below line of code in both tests.
    expect(todoElement).toContainHTML("")
    I get the error saying
    expect(element).toContainHTML()
    Expected:

    Received:
    Make dinner
    Can you please tell me why it isn't working for me?

    • @BumLowkey
      @BumLowkey 2 ปีที่แล้ว +3

      try replace "" by "strike", I think different versions

  • @aprimic
    @aprimic 2 ปีที่แล้ว +1

    you have the voice of Ross from friends, not the accent though

  • @iliketocode6986
    @iliketocode6986 2 ปีที่แล้ว

    So annoying how they change the api

  • @PierreLaBaguette
    @PierreLaBaguette 2 ปีที่แล้ว

    my 2 cents - I believe that a to-do item would typically be an item in a list, therefore don't use DIV tag but LI (list item). it will also help your accessibility efforts and spread good practices and love :)
    edit: the same with the h1 element use - h1 is a main header of a page, Header level 1. pages should typically have only one element like that. why not use just P, Span or Strong instead? (check udacity course about Web Accessibility)
    + I'd love to see how you test state changes of elements, especially of parent and child element, each of them having co-related states

    • @jackstorm777
      @jackstorm777 ปีที่แล้ว +1

      It's a course on unit testing, not HTML best practices :)

    • @PierreLaBaguette
      @PierreLaBaguette ปีที่แล้ว +1

      @@jackstorm777 (bad) habits have high chances of becoming the default way of doing things. one might forget the good practices if they don't get applied when possible

  • @ax3lgarcia675
    @ax3lgarcia675 ปีที่แล้ว

    why sompeople speak about .babelrc or babel.config.js ? you don't speak about that

  • @jerodnilstead5985
    @jerodnilstead5985 ปีที่แล้ว

    nice tutorial, back up from the mic a bit, hearing your mouth function is quite unpleasant.

  • @NavySturmGewehr
    @NavySturmGewehr ปีที่แล้ว

    The lip smacking is just terrible. Why do people do that? I really want to watch your content but the lip smacking just drives me insane. It is so avoidable, it's like people don't take the time to listen to their audio before posting.

  • @Human_Evolution-
    @Human_Evolution- 2 ปีที่แล้ว

    Not a fan of the const H1 stuff. Nope. And I hate tests. I think I hate every testing video on TH-cam.

  • @paridokhtalavinia
    @paridokhtalavinia 2 ปีที่แล้ว +5

    Hi, thank you for this video, but expect(todoElement).toContainHTML('') is not working seems it should be expect(todoElement).toMatchSnapshot('');

    • @filcondrat
      @filcondrat 2 ปีที่แล้ว

      yep

    • @Shaktish-kumar
      @Shaktish-kumar 2 ปีที่แล้ว +2

      expect(todoElement).toContainHTML('strike');
      This worked, use only the html tag name

  • @anmolgupta1407
    @anmolgupta1407 3 ปีที่แล้ว

    Hi,
    Can I have your email id? It was not mentioned inside the 'About' section of your channel.

  • @456fghbsrtywrtwre
    @456fghbsrtywrtwre 11 หลายเดือนก่อน

    Nice explanation. Although expect(todoElement).not.toContainHTML(''); this test does not work.

  • @IncolasCopperfield
    @IncolasCopperfield 2 ปีที่แล้ว +1

    thank you