How to Mock Fetch in Jest Manually

  Рет қаралды 39,818

Leigh Halliday

Leigh Halliday

Күн бұрын

Пікірлер: 60
@andrewhulme1188
@andrewhulme1188 3 жыл бұрын
Thanks for the video Leigh, very helpful For anyone having issues where it returns undefined, I solved it by putting the 'global.fetch....' INSIDE the 'it' function (or in a beforeEach)
@leighhalliday
@leighhalliday 3 жыл бұрын
Thanks Andrew! Thanks for sharing how you solved the issue!
@harikrishnavholla5036
@harikrishnavholla5036 2 жыл бұрын
Thank you
@jesusgodinho3478
@jesusgodinho3478 3 жыл бұрын
Not all heroes wear capes, quite literally. I've been looking to carry out the exact same tests for my API. True legend !
@leighhalliday
@leighhalliday 3 жыл бұрын
Haha thanks! Check out my video on mock service worker, you’re going to love it!
@eestiprogrammeerija1244
@eestiprogrammeerija1244 2 жыл бұрын
THANK YOU SO MUCH MAN, you save a lot of time for me, i was googling this for 2 days with no certain answer, thanks
@dawid_dahl
@dawid_dahl 4 жыл бұрын
Finally a good testing tutorial! Thank you!
@leighhalliday
@leighhalliday 4 жыл бұрын
Hey Dawid! Check out the video I just published about Mock Service Worker, I think you'll like it!
@MrVisheshsingh
@MrVisheshsingh 4 жыл бұрын
Thank you! Keeping my 👀 open for the upcoming videos
@leighhalliday
@leighhalliday 4 жыл бұрын
Coming tomorrow and Sunday :)
@abessesmahi4888
@abessesmahi4888 4 жыл бұрын
I'am so excited to see the upcoming videos on testing Thank so much sir.
@leighhalliday
@leighhalliday 4 жыл бұрын
Thanks Abesse! One today another tomorrow! :)
@TheDoctorVideos
@TheDoctorVideos 4 жыл бұрын
Great video, really helped me understand how the global fetch and mocking works!
@leighhalliday
@leighhalliday 4 жыл бұрын
Awesome!! Happy it helped :)
@takeshikriang
@takeshikriang 3 жыл бұрын
This is really good Leigh. Thanks for sharing.
@pravinpoudel1307
@pravinpoudel1307 3 жыл бұрын
Hello Leight on API request, i get error response of 'You have not supplied an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY]' , is it just me or there is another way to access the API?
@nikosspiropoulos8417
@nikosspiropoulos8417 2 жыл бұрын
the global function fetch now returns undefined when i assing jest.fn to the global object. ANY IDEAS/??????? thanks in advance
@dominikseljan3043
@dominikseljan3043 4 жыл бұрын
Great one Leigh! Can't wait to see other two videos.
@leighhalliday
@leighhalliday 4 жыл бұрын
Thanks Dominik!! One today another tomorrow :)
@umhurram
@umhurram 2 жыл бұрын
Thanks Leigh!
@ArtiomNeganov
@ArtiomNeganov 2 жыл бұрын
Very clear explanation. Thank you!
@nicholassmith6412
@nicholassmith6412 2 жыл бұрын
Excellent video
@ibadshaikh2215
@ibadshaikh2215 2 жыл бұрын
How to make sure that the fields coming from API response always remain unchanged. Like if the returned object is { rates : {} } , how can I know that the rates key is always there.
@Ls-xb2bn
@Ls-xb2bn Жыл бұрын
Ur a god. Thanks!!!! Really thanks. This was so usefull
@average-user9
@average-user9 3 жыл бұрын
thanks! and, it would be cool to have a link to second video in the end, and so on) thanks anyway)
@ahmedjumaah9727
@ahmedjumaah9727 3 жыл бұрын
Hi Leigh, what if we have a react useState and want to pass the response to the state, how would we test this scenario ? Thanks
@MageLink
@MageLink Жыл бұрын
Dude, you rocks
@larube
@larube 4 жыл бұрын
Another good video !
@leighhalliday
@leighhalliday 4 жыл бұрын
Thank you Amador :D I appreciate the support!
@larube
@larube 4 жыл бұрын
@@leighhalliday You deserve it !
@MohieddineSaadeh
@MohieddineSaadeh 2 жыл бұрын
Thanks for the video.. I followed the steps but funny enough reject failed my test even if I remove all expects.. Also I'm using NextJS and showing an error message on the screen which is not showing.. I know it is async to begin with, but I did await fetch in my code so basically the rendered HTML should have updated.. Anyways will dig into that.. Thought of sharing my experience.
@sushmak3110
@sushmak3110 3 жыл бұрын
Hi Leigh, could you please guide how to mock sequalize ORM on sqlite db
@leighhalliday
@leighhalliday 3 жыл бұрын
Hey Sushma! Perhaps :D If it's something I run into in my development, I'll definitely do one.
@davidobodo3605
@davidobodo3605 2 жыл бұрын
thanks a lot for this
@akamfoad
@akamfoad 4 жыл бұрын
thanks great video Leigh, but something is weird, global overriding don't work in my project when doing them outside it/test blocks, made with CRA as well as yours, I saw other projects also using global overriding outside it/test blocks and works well, it seems you guys tweaked something or what is wrong if not?
@leighhalliday
@leighhalliday 4 жыл бұрын
Hey Akam! Sorry... I am not really sure! Perhaps something has changed since the time this video was released... I believe I link to the source code... can you find anything there that might be why?
@stevereid636
@stevereid636 4 жыл бұрын
I tried following using typescript. I used this: const globalAny: any = global; globalAny.fetch = jest.fn(() => Promise.resolve({ json: () => Promise.resolve({ rates: { CAD: 1.42 } }) }) ); and it worked fine for a time, but when I got to the mockImplementationOnce() , typescript complained that "Property 'mockImplementationOnce' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise'." Any ideas on how to resolve this?
@leighhalliday
@leighhalliday 4 жыл бұрын
Hey Steve! Sorry, I am not great with TypeScript... I personally don't know how, but maybe someone else does here? Maybe you can redefine the type of fetch to be a union of whatever type fetch normally has combined with the type of a fetch mock function?
@stevereid636
@stevereid636 4 жыл бұрын
@@leighhalliday Thanks for the suggestion anyway 👍🏾
@bindushree1037
@bindushree1037 3 жыл бұрын
Can you make a video on how to mock the post fetch request which connected to backend having url like localhost:8080 using jest
@leighhalliday
@leighhalliday 2 жыл бұрын
Hey! I would check out "mock service worker" for this sorta thing.
@alanthomasgramont
@alanthomasgramont 4 жыл бұрын
How do I mock a "service" which isn't using fetch? That service could just be a function call that is may be doing all sorts of things I don't want to test here. import { computeData } from '../services/DataService' I want to replace computeData for all my tests within the component that I'm testing. So my component is calling something like: useEffect(() => { setData(computerData(props.a, props.b)); },[props.a, props.b]) and when data is set, it renders a table or something. computerData isn't a math function or anything and every test might need a different computed result so I don't want to use __Mock__
@leighhalliday
@leighhalliday 4 жыл бұрын
Hey Alan! I would use a manual jest mock but then you can override it with jestjs.io/docs/en/mock-function-api#mockfnmockimplementationoncefn whenever you need it to do something specific for a single test.
@bscodev
@bscodev 3 жыл бұрын
Thanks Master!
@chetanjain4616
@chetanjain4616 3 жыл бұрын
Hey, sorry but I still don't get it, you are still fetching the API right? when , on line number 10, you call convert function it still fetches the API and return the results which lets say if I turn off my internet would result in a failed test right?
@nimatsergiu2942
@nimatsergiu2942 4 жыл бұрын
Thanks man this really helped me out ;)
@leighhalliday
@leighhalliday 4 жыл бұрын
Glad I could help, Nimat! Thank you!
@ImranKhansilvake
@ImranKhansilvake 4 жыл бұрын
soo good. thankyou very much
@leighhalliday
@leighhalliday 4 жыл бұрын
Thanks Imran! I appreciate it!
@SheryarShirazi
@SheryarShirazi 4 жыл бұрын
I'm not getting intellisense like it/test in VSCode react anyone knows?
@leighhalliday
@leighhalliday 4 жыл бұрын
Hmmm... sorry, I'm not sure Sheryar!
@bigk9000
@bigk9000 3 жыл бұрын
Okay, no matter what I do, no matter which method I employ or what package I install, I cannot mock fetch. For whatever the reason, it's utterly impossible.
@leighhalliday
@leighhalliday 3 жыл бұрын
Hey King! Give this a try... it's my preferred way to do it now: kzbin.info/www/bejne/rGial52hgLaHfpI
@rajat420420
@rajat420420 4 жыл бұрын
How to mock superagent?
@leighhalliday
@leighhalliday 4 жыл бұрын
Hey Rajat! I think you could probably use the approach in this video which is less about mocking and more about intercepting HTTP requests, which would work for fetch/axios/superagent/etc... kzbin.info/www/bejne/rGial52hgLaHfpI
@DarrylHebbes
@DarrylHebbes 4 жыл бұрын
part time forex trader?
@leighhalliday
@leighhalliday 4 жыл бұрын
Hehe nah, it's just hard to find a simple API to use in examples that doesn't require an API key :D
Mocking Fetch in React Components
14:58
Leigh Halliday
Рет қаралды 10 М.
Mocking Axios in Jest + Testing Async Functions
17:43
Leigh Halliday
Рет қаралды 83 М.
HELP!!!
00:46
Natan por Aí
Рет қаралды 69 МЛН
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 137 МЛН
Car Bubble vs Lamborghini
00:33
Stokes Twins
Рет қаралды 44 МЛН
小丑揭穿坏人的阴谋 #小丑 #天使 #shorts
00:35
好人小丑
Рет қаралды 54 МЛН
Mock vs Spy in Testing with Jest: Which is Better?
25:12
Dev tips by MoHo
Рет қаралды 12 М.
Mocking Asynchronous Functions with Jest
21:50
Swashbuckling with Code
Рет қаралды 72 М.
Mocking a Database in Node with Jest
13:29
Sam Meech-Ward
Рет қаралды 68 М.
Mocking Fetch Using jest-fetch-mock
10:27
Leigh Halliday
Рет қаралды 17 М.
Introduction to Jest Testing | JavaScript Unit Tests
25:30
Dave Gray
Рет қаралды 32 М.
JavaScript Testing - Mocking Async Code
18:05
Academind
Рет қаралды 146 М.
Testing Asynchronous Components with Mocks in Jest
23:22
Leigh Halliday
Рет қаралды 36 М.
Unit Testing in React with Jest - Mock, Snapshots and more
26:19
Coding With Abbas
Рет қаралды 18 М.
Why Vitest Is Better Than Jest
13:13
Web Dev Simplified
Рет қаралды 138 М.
React Testing Library - Testing a mock Axios request
15:22
Leigh Halliday
Рет қаралды 47 М.
HELP!!!
00:46
Natan por Aí
Рет қаралды 69 МЛН