Mocking React Components and Functions using Jest for Beginners - React.js Testing Tutorial #5

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ย. 2024

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

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

    Hi Brune, Really find this tutorials on testing helpful especially for a beginner into testing like me. Could you recommend another means of mocking react components as mocked - ts-jest/utils has been deprecated?😁

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

      That's awesome to know my friend 😍
      Yes, absolutely, It was deprecated 12 days ago =D
      Basically ts-jest/utils is now deprecated because it was moved (copy pasted) from ts-jest to jest itself (which is great news if you ask me)
      You'll be able to do ``` jest.mocked(SomeComponent) ``` and it works exactly as before, because it's the exact same code :)
      Just keep in mind that there was a mistake in that Pull Request and they forgot to export the mocked function. They have now a new PR opened, but not merged yet:
      PR: github.com/facebook/jest/pull/12133
      Issue: github.com/facebook/jest/issues/12131
      If you give them a few more days, then you can move from ts-jest/utils to jest itself :)

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

      @@BrunoAntunesPT Hi Bruno, Thanks for the quick response. I’ll take your advise and wait a few more days. 😁

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

      @@aolausoro2942 yeah, I will be doing a "find and replace" kind of job when they merge that one 😅😅😅
      For now, nothing to worry about 😉😉 just subscribe to that GitHub issue/PR and when it gets merged it's party time 😂😂😂

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

      @@BrunoAntunesPT Thank you for the awesome tutorial! I have learnt a lot from this single video and I can contribute directly to my project.
      However, jest.mocked() still does not work
      jest.mock('material-table', () => ({
      ...jest.requireActual('material-table'),
      MaterialTable: jest.fn(() => Table),
      }));
      const mockedMaterialTable = jest.mocked(MaterialTable);
      ● Test suite failed to run
      TypeError: jest.mocked is not a function
      35 | }));
      36 |
      > 37 | const mockedMaterialTable = jest.mocked(MaterialTable);
      | ^
      This is the error I am getting. Can you please help me with this?

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

    Thank the lord for this guy 👏I'm a junior dev' and I started working for a big company three months ago. I had no experience of testing and it has severely affected my performance in this role, I wish I had have seen this video two months ago but it's still useful for me. Lots of other jest videos out there are good, but pretty basic, this one is up to date and exactly how my company mocks so cheers Bruno.
    P.s. I know it sounds like I'm this dude's friend giving him a nice review, but I don't care this video is deffo worth it for anyone who doesn't have a goosy about testing like myself a short while ago.

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

      Thank you very much for your words Danny ❤️😍

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

      Hmmm this comment has inspired me to watch

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

      Rightly said! This covers the concepts which we actually require in our day-to-day work in the company projects.

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

    This is the best testing video I've ever seen!

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

    Hi Bruno, there are lots of testing tutorials on youtube but your tutorials are one of the best. Thank you for your great explanations. You took a break from youtube videos, I think you should continue. We're waiting for your new videos.

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

      Thank you very very much for your words 🙂 currently I don't have the time to publish, but hopefully one day I'll be able to come back to TH-cam 😍

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

    This is by far the best tutorial on mock/spy that i have found. Thanks

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

    Amazing tutorial, better than the jest documentation itself, mocks are so powerful!

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

    Thank you Bruno for this wonderful series! 🤓👍👍👍

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

      Thank you very much 😊❤️
      Monday we will have video 6, showing how to mock http calls using msw 😊

  • @PraveenKumar-ft2kr
    @PraveenKumar-ft2kr 2 ปีที่แล้ว +1

    You gave my motivation back brother... Thank you so much :) So happy that i found your channel :)

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

      So happy to read that my friend 😍😍

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

    El mejor tutorial de pruebas que he visto en la red. Te agradezco muchísimo Bruno!!

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

    Finally, I've understood what spying and mocking is! Thanks!!!!

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

    Super professional tutorial I've seen so far. Really many practical tricks explained here which our team spent years to figured out with Jest.
    Still looking for a trick to save memory when jest is running.

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

      Thank you very much 😍 regarding memory I have never faced anything too bad (probably having 32gb of ram helps me 😅😅)

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

    Thank you so much! Your manner of explanation is great!

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

    Oh my gosh, I spent all day yesterday looking at articles on how to do this and got nowhere. 6 minutes into your video and I have a passing test! Thanks!

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

      I'm sorry to hear that you spent a day looking for this information. On a positive note, I'm very happy you found value in my video 😍😍😍 thank you very much ❤️

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

    This is an amazing series on test.
    Simply wonderfull

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

    Love your presentation man. awesome !!!

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

    Loved it, Thank you :)

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

    Excellent explanation. Thanks!

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

    Really helpful as always Bruno. Thank you !

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

    This guy is something else...

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

    Liking this before watching. Been a while since I have found one of your videos.

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

      Thank you very much - I hope you have enjoyed the video after watching it 😅

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

      @@BrunoAntunesPT Always.

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

      so good to read your comment 😍😍 once again, thank you very very much 🤗

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

    Excellent explanation, you gained another subscriber, congratulations!

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

    Thanks awesome tutorial

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

    What a great video. Thanks for putting this together. Nice and clear

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

    Very useful tips! Thanks! :)

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

    This is very informative! Thank You!

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

    Very helpful, esp the last trick. Thank you.

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

    Hello, this is pretty clear and helpful, just one question , what happen if I m not using typescript, in minute 12 more or less you used mocked() that comes from ac ts library, I did not understand it a lot, if it is a normal js file how can I use it? :)

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

    You videos are so great ☺️

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

    Thanks bro!

  • @RaviKumar-hz8cf
    @RaviKumar-hz8cf 7 หลายเดือนก่อน

    Can i use jest.spy on in different js which is not my test file file or we have to use it in only test.js file and inside describe and test block only.

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

    That was good! Will we have a video about mocking HTTP calls from the component. Or basically, this is the same as mocking async func calls that you described in the previous videos

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

      Thank you very much my friend 😍
      That will be Monday's video (video 6) 😊
      we will be using msw (mock service worker) to help us mocking the http calls, then as you said, we will use also the concepts learned in Video 4 (async testing) 😊

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

    Awesome

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

    Hey Bruno, awesome video, you mocked the Dataggrid with mocked function from ts-grid, but I am not using typescript in my project. How to mock it then and also what is the significance of mocking here with ts-grid when you are mocking it with jest.mock ??

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

    Hey man, nice video, thanks a lot for it. I'd like to know if you know how to deal with a situation that I have the component displayed twice or more inside a component, and I want to validate the params sent to both? Like:

    How would I test those two components separately if when I mock it, I mock both at the same time?
    I appreciate man =) The way you explain is simple and didactic, loved it.

  • @CristianRodriguez-zn4wp
    @CristianRodriguez-zn4wp 3 ปีที่แล้ว +1

    Thanks!

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

      Thank you soo soo much Cristian 😍❤️

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

    Thank you for this great instruction. By the way,
    Does the same concept apply to the react native applications?

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

      Thank you 🎉 uhhhhh good question... I have never used react native. I would assume yes, but I might be massively wrong 😅

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

    Hi Bruno, thank you for posting this for us! I really like your tutorials.
    May I ask one question:
    Why do you use "mocked" from "ts-utils" for mocking the DataGrid, but it was not necessary for mocking the VeryComplex component? When do I know if I need to use the "mocked" from ts-utils?
    Thanks!

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

    Great video!
    But I have a problem. What do I use instead of the "mocked" function, given I'm not using Typescript and therefore am not using "ts-jest/utils"? Thanks

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

    first comment 😂😍

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

    Thanks

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

    Hello Bruno! Your video is so helpful, thx! I have one question.
    Can I mock my own component or component from any library only in specific tests?
    Cause now I unfortunately didn’t find a correct way and my solution is to create two test files with mock and without, so, it is no a better solution 😄

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

      Thank you very much 😍
      When you do jest.mock you are mocking that dependency for all the tests in that file. I honestly never found a use case where inside the same file I want to have mocks for some tests and no mocks for other tests - but it's very possible that I'm understanding your request in a wrong way 😅

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

    Second first comment 😋

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

      Ahah 😅 second first 😂 thank you Ricardo ❤️

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

    So awesome materiallll broooo

  • @Kevin-yw5qr
    @Kevin-yw5qr 3 ปีที่แล้ว +1

    Thanks!

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

      You are amazing Kevin ❤️ thank you very much 😍