Mocking Asynchronous Functions with Jest

  Рет қаралды 72,827

Swashbuckling with Code

Swashbuckling with Code

Күн бұрын

Пікірлер: 117
@sharonfitzpatrick5179
@sharonfitzpatrick5179 3 жыл бұрын
This is by far the best video I've found explaining mocking Jest
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Awesome, I'm glad to hear that.
@ShubhamKumar-mk6zb
@ShubhamKumar-mk6zb 9 ай бұрын
I prefer blogs over videos, thankyou for making one and very understandable.
@mateuszszaowicz4373
@mateuszszaowicz4373 Жыл бұрын
This is the best video about mocking I have encountered, it really made me understand how it works. Big thanks to you sir!
@imekachi
@imekachi 2 жыл бұрын
Thank you for taking us into traps and teaching us how to escape/fix them. Short, precise, and easy to understand, also with sections that you can jump around and quickly learn what you need. 👍 Amazing.
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
I appreciate the feedback and knowing that the effort was useful. Cheers!
@tachylamurray9452
@tachylamurray9452 2 жыл бұрын
God Bless you or whatever. I lost a whole day trying to figure out how to call my test async. So true how "it might not be so obvious to some" Thank You!
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Happy to have helped.
@jimmymac601
@jimmymac601 3 жыл бұрын
I will personally hunt down anyone who downvotes this tutorial. Outstanding presentation.
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Haha, ty.
@jonathanlinat
@jonathanlinat Жыл бұрын
This was the most useful video I ever watched about mocking functions using Jest. You've made it pretty simple to understand the concept. Nice job Jimmy!
@arfasaif6139
@arfasaif6139 2 жыл бұрын
one of the simplest and well-explained clean video of jest mocking so far! thanks!
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Thank you for the kind words. Always nice to hear that your efforts were appreciated.
@LeoMuzi
@LeoMuzi 2 жыл бұрын
Thank you so much! This is really useful to understand how mocking works under the hood, helps you thinking of what's going on to figure out issues with testing. Helped me a lot.
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
lovely
@petergoodey
@petergoodey 2 жыл бұрын
We have struggled with axios mocking on a complex React/Typescript project having only recently up-skilled. Best explainer!
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Thank you for the kind words and I'm happy to hear your skills are improving.
@viridianite
@viridianite 2 жыл бұрын
Thank you so much, Jimmy! You're slowly demystifying testing JS code with Jest for me!
@SwashbucklingwithCode
@SwashbucklingwithCode Жыл бұрын
That's what I like to hear. Thank you for letting me know.
@viridianite
@viridianite Жыл бұрын
@@SwashbucklingwithCode You've a talent for pedagogy. Thanks for your video on asynchronous JS. I'm looking forward to your future videos :) I've a few topic suggestions: * REST API using express.js and some database library This could be a multiple parts series like your CI/CD series. You could show how you'd write the tests for it (e.g., unit tests, integration tests, and e2e tests), consume it with something like Axios, etc. * GraphQL API
@vikram87in
@vikram87in 2 жыл бұрын
The way you teach is fantastic! And yes, this is the best video on the topic.👏👏 Thank you so much
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
I really appreciate that, thank you.
@giachan4528
@giachan4528 2 жыл бұрын
this video is short and easy to understand, please make more videos for unit testing, it is helpful for a beginner like me. Thanks!
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Glad it worked for you.
@aglowkeys
@aglowkeys 3 жыл бұрын
Thank you! This was so useful to me. Gonna be watching some more of your Jest videos now. :)
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
happy it was useful. ty for letting me know.
@SwOOp_de
@SwOOp_de 3 ай бұрын
Great video...thanks for explaining jest mocks.
@CristianAndresNietoGarcia
@CristianAndresNietoGarcia 3 жыл бұрын
An excellent resource in my current journey in Unit testing
@Alan.livingston
@Alan.livingston 2 жыл бұрын
Needs to be more stuff of this quality on the net. Most explanations are too simplistic and are aimed at people with no experience.
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
I agree, and thank you. I can empathize more now with the lack of intermediate videos because they are quite challenging to figure out how much explanation to put in without making the videos crazy long.
@saprone8885
@saprone8885 2 жыл бұрын
This is great and helped me a lot understanding async mocking and got it working in my own project! I am happy thanks for this :)
@kiberslav
@kiberslav 2 жыл бұрын
Very Useful video. Nice explaining 👏Thank you. Please keep it up with testing videos, this is gold.
@alfredmadere9030
@alfredmadere9030 2 жыл бұрын
Great vid bruv. Make another about standard practices for mocking models and such
@emmanuelsackey3026
@emmanuelsackey3026 3 жыл бұрын
Thank you. You earned a subscriber at "Sanity check" . Awesome bro.
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Thank you.
@hhdev9954
@hhdev9954 3 жыл бұрын
Very good explained 💡, thank you Jimmy!😊
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Awesome.
@Bregylais
@Bregylais Жыл бұрын
Subscribed, liked, and alas commented. Beautifully explained!
@bikizzle
@bikizzle Жыл бұрын
Thank you for this life saver video. This is great knowledge shared
@Andrans1
@Andrans1 3 жыл бұрын
Amazing tutorial, thanks for sharing your knowledge
@claudiobaumgartner2760
@claudiobaumgartner2760 3 жыл бұрын
Super interesting! Thx for the nice tutorial 🔥
@JoseciusDev
@JoseciusDev Ай бұрын
Thanks for this content. (from India )
@gordemn
@gordemn 2 жыл бұрын
Wow I was really struggling with this but got it to work thanks to your videos. Thank you js Jesus
@mubashirarif5792
@mubashirarif5792 3 жыл бұрын
Sir big fan...CodeStackr named you in his video on monday and I am since then loving your content....
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Thank you, I greatly appreciate that.
@OhReallyHuman
@OhReallyHuman Жыл бұрын
"...test properly fails..." that line is hilarious and scary at the same time haha.
@santhoshraghavpidathala3701
@santhoshraghavpidathala3701 3 жыл бұрын
Please continue to teach jest and react testing library
@matthieu1551
@matthieu1551 3 жыл бұрын
Very interesting. Thanks a lot 🤩
@j.ignacior.p.9481
@j.ignacior.p.9481 10 ай бұрын
Awesome video. Thank you very much.
@Huey-ec1
@Huey-ec1 5 ай бұрын
Great explanation
@aashisharyal9704
@aashisharyal9704 Жыл бұрын
Amazing 👏👏 Thank you so much 💕
@suryapratap9915
@suryapratap9915 Жыл бұрын
@SwashbucklingwithCode Thank you so much for the in depth explanation. What if my function is having multiple axios calls and these axios calls are wrapped in another function called 'makeApiCall'.
@SwashbucklingwithCode
@SwashbucklingwithCode Жыл бұрын
If you have a function that abstracts all the fetching, it actually makes it easier to mock because you can just mock that module itself. Just make sure it is exported, and I usually put it in it's own file.
@adrianjason13
@adrianjason13 2 жыл бұрын
Helpful content, thanks! What VSCode extension are you using for intellisense/autocompleting Jest code?
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
I believe that was shown in a previous video in this videos playlist, but it comes from installing @types/jest
@programandocomandersonsouza
@programandocomandersonsouza Жыл бұрын
Excelent. Thank you so much.
@zacalves
@zacalves 3 жыл бұрын
great tutorial! thanks a lot for that!
@dharmsingh6924
@dharmsingh6924 3 жыл бұрын
Nicely explained 👍
@el.vilchez183
@el.vilchez183 Жыл бұрын
Thanks Jesus for teaching mocks :D
@mahmoudnasser2158
@mahmoudnasser2158 2 жыл бұрын
What about if you have multiple gets to different endpoints and each return different information? How could you mock each api get separately to tell Jest “this is the data you should expect for api A but another data for API B”?
@ErikBackman242
@ErikBackman242 2 жыл бұрын
e.g. mockimplementationonce
@haralc
@haralc 3 жыл бұрын
Thank you Jesus for this tutorial! 🤣
@johannyberg4988
@johannyberg4988 3 жыл бұрын
Good job!
@y_thedreamer95
@y_thedreamer95 2 жыл бұрын
which font do you use at WSL? it looks awesome!
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Cartograph Mono CF.
@creative-commons-videos
@creative-commons-videos 2 жыл бұрын
Not always a response have success but sometimes it throws an Error too, so how can we mock the errors and handle it ?
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Lovely question. I'll likely do a part 2 of this video for error states and multiple endpoints. In the meantime, I'm sure other comment readers would love to here your (and others) answer after you take a stab at it.
@shwackthenoobsac
@shwackthenoobsac 2 жыл бұрын
Thank you!
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
Happy to help.
@DevTomazetti
@DevTomazetti Жыл бұрын
Awesome video! How can I mock the error case?
@lifeisbeautifu1
@lifeisbeautifu1 Ай бұрын
Thank you
@mohammedamin6859
@mohammedamin6859 3 жыл бұрын
awesome stuff man, can you create react-centric testing tutorials?
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
I've been pondering on that for a little bit. I've had a few requests, but I'm not sure how much I'd be stepping on Kent C Dodds toes, since I use his library and he has a bunch of tutorials for it. Thanks for the request, I'll definitely be thinking on it.
@HarelTussi
@HarelTussi 3 жыл бұрын
Awesome content, can you please upload more videos on jest and react-testing-library
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Unit Testing and Jest are likely the next series I'll be working on. I've had a few requests for testing-library, so I'll probably throw that in the mix.
@sameerizaj5458
@sameerizaj5458 Жыл бұрын
sir please answer me the thing is the api part i did understand the thing is i'm a backend dev right so i'll be testing the endPoint via your method right i'll not actually call the function since it'll be a endppoint and second thing is that what if i want to mock the function that interacts with mongdb and returns the promise so my question my team lead said you don't need to import the mock service file in which just mock the functions so that means that i''l allo do the same like just resolved the value that function return and also in my some spis i've very complex data like array which contains the objects and each might have the array nested so that's why i'm asking please help me i've deliverd the project tommorow
@santhoshraghavpidathala3701
@santhoshraghavpidathala3701 3 жыл бұрын
Thanks a lot, I am about to ask jest and react testing library. Could you please teach promises with API not with settimeouut function
@PrieyudhaAkaditaS
@PrieyudhaAkaditaS 3 жыл бұрын
Thankyou
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Thanks for stopping by. Best of fortune to ya.
@oscareberle744
@oscareberle744 3 жыл бұрын
Thanks Jesus for this video! :)
@sivaram7115
@sivaram7115 Жыл бұрын
what if we have 2 get api calls in the react component. How to return different values for both
@SwashbucklingwithCode
@SwashbucklingwithCode Жыл бұрын
I believe you can down some narrowing based on the endpoint path, but how I prefer to handle this is make a module that exports a bunch of api calls as functions. I name them based on what they do. This way, you can mock the entire function.
@EulerAlvarenga1
@EulerAlvarenga1 2 жыл бұрын
I'm using typescript. When I call the real api the test works but when I call the mockImplementation it always returns undefined =/
@viridianite
@viridianite 2 жыл бұрын
You should be resolving the returned promise to some value. By default, Promise.resolve() resolves to undefined.
@sauravthakur6261
@sauravthakur6261 Жыл бұрын
make a video on redis mock get set functions
@WebDevwithTed
@WebDevwithTed 2 ай бұрын
So mock is to test it "fake" and then end to end for real test?
@Nurtylek
@Nurtylek 3 жыл бұрын
great!
@Emma-eb5zb
@Emma-eb5zb 2 жыл бұрын
How can I test a function that calls multiple other functions?
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
You'd call it just the same as any other function. You likely should individually test those subfunctions as well. The only real issue is when there are side-effects happening with any function, and that's a situation where you either should mock or rewrite the function to not cause side-effects (depending on limitations).
@aspirinemaga
@aspirinemaga 2 жыл бұрын
Very informative, but it seems won't work in my case for no reason
@Chavez3d
@Chavez3d 2 жыл бұрын
is lemmy kilmister your dad? also great video, sub'd!
@cla1814
@cla1814 3 жыл бұрын
you should post source code in the video description.
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
In most videos I do, but this one was based off a blog post which has the required code. The complete test code is at the bottom, but I could see how it would be more convenient to have files to follow around.
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Added this in the description, thanks for letting me know: github.com/Jimmydalecleveland/jest-mock-async-example
@cla1814
@cla1814 3 жыл бұрын
@@SwashbucklingwithCode Always good idea to post code, even if at least github gist, that way If someone is coding alone can compare their code or save it for future reference.
@80Vikram
@80Vikram 2 жыл бұрын
Jesus is real and he loves TDD. Thanks for saving developers' lives US Jesus
@elsetiyawan
@elsetiyawan Жыл бұрын
I feel like Jesus teach me testing.
@methmaaravinda5107
@methmaaravinda5107 9 ай бұрын
oh jesus
@moacirsouza5343
@moacirsouza5343 2 ай бұрын
Jesus!
@reasonforge9997
@reasonforge9997 2 жыл бұрын
Lots of mocking in this video...but it's all in Jest.
@SwashbucklingwithCode
@SwashbucklingwithCode 2 жыл бұрын
I see what you did there.
@shadev23
@shadev23 2 жыл бұрын
Jesus ???? It's you ???
@yashkaliapiano
@yashkaliapiano Жыл бұрын
Jesus teaching software testing
@erenyeager7465
@erenyeager7465 Жыл бұрын
Jesus
@hendra5604
@hendra5604 3 жыл бұрын
First ☝🏻😅
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
You are indeed.
@hendra5604
@hendra5604 3 жыл бұрын
@@SwashbucklingwithCode What theme used there? Deep ocean?
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
@@hendra5604 This is actually my own theme called Everset. marketplace.visualstudio.com/items?itemName=jimmydc.everset
@hendra5604
@hendra5604 3 жыл бұрын
@@SwashbucklingwithCode Perfect, many thanks. I just hated purple's around material theme. Created my own theme for personal use, had hard time to distinguish color between html, json, php, ts/js, py, yaml.
@serial_coder
@serial_coder 7 ай бұрын
Jesus Christ
@jimlatsko4804
@jimlatsko4804 3 жыл бұрын
Can you please help with converting this to typescript? Specifically the line mockAxios.get.mockImplementation(() => Promise.resolve({data: {name: "Jimmy Jedi"}})); since mockImplementation generates the following compiler error: TS2339: Property 'mockImplementation' does not exist on type ' >(url: string, config?: AxiosRequestConfig) => Promise '.
@jimlatsko4804
@jimlatsko4804 3 жыл бұрын
With help from stackoverflow, got this working: here are some changes to consider for typescript: import axios from "axios"; const mockAxiosGet = jest.spyOn(axios, 'get'); mockAxiosGet.mockResolvedValue({ data: {name: 'Luke Skywalker'}});
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
Have you already installed @types/jest ?
@jimlatsko4804
@jimlatsko4804 3 жыл бұрын
@@SwashbucklingwithCode yes.
@SwashbucklingwithCode
@SwashbucklingwithCode 3 жыл бұрын
@@jimlatsko4804 Hmmm I don't recall getting an error in TS for mock methods. You might have to set axios with teh `as` keyword to jest.Mock or something like that.
@EulerAlvarenga1
@EulerAlvarenga1 2 жыл бұрын
I had the same issue, With help from stackoverflow, got this working: here are some changes to consider for typescript: import axios from 'axios'; // import axios instead of mockAxios jest.mock("axios"); const mockedAxios = axios as jest.Mocked // head up here, I changed the name of the var
Reveal testing weakspots in your JavaScript code with Jest Coverage
53:43
Swashbuckling with Code
Рет қаралды 11 М.
Mock vs Spy in Testing with Jest: Which is Better?
25:12
Dev tips by MoHo
Рет қаралды 12 М.
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,8 МЛН
Can You Find Hulk's True Love? Real vs Fake Girlfriend Challenge | Roblox 3D
00:24
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 11 МЛН
Testing with Jest: From zero to hero
36:56
LogRocket
Рет қаралды 63 М.
Mocking Axios in Jest + Testing Async Functions
17:43
Leigh Halliday
Рет қаралды 83 М.
Mocking a Database in Node with Jest
13:29
Sam Meech-Ward
Рет қаралды 69 М.
JavaScript Testing - Mocking Async Code
18:05
Academind
Рет қаралды 146 М.
The ONLY REASON To Unit Test
8:26
Theo - t3․gg
Рет қаралды 84 М.
The Value of Source Code
17:46
Philomatics
Рет қаралды 204 М.
Do NOT Learn Kubernetes Without Knowing These Concepts...
13:01
Travis Media
Рет қаралды 325 М.
Testing Serverless Functions with Jest & Next.js API Routes
20:30
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,8 МЛН