Mock HTTP calls using Fetch or Axios - Mock Service Worker - React.js Testing Tutorial #6

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ม.ค. 2025

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

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

    You have no idea of how this video saved me a huge headache! Thank you so much! 🙏

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

    Amazing video Bruno!! And super detailed!

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

      Thank you very much Telmo ❤️❤️
      I'm very happy to see you here! We need to do something together one day 😉😉

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

    Your content is fantastic. Thank you so much for your efforts!!!!!

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

      Thank you very very much Harune 😍😍❤️

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

    Thank you so much Bruno for your amazing tutorials. I have waited for you will show MSW mocking in testing series.
    Pls. Next would be Cypress with msw and new cypress component testing feature

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

      Thank you very much ❤️
      I have a few videos left in this series before I think in the next one 😅

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

    Thanks! :) Keep them coming!

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

    Brumo, you are my only hope now, i need to learn testing redux toolkit query, plz if u could make a 10000000 hrs video i'll be more than happy lol

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

    Thank you so much Bruno, your content is amazing!

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

    loved your content and way of explaining concepts..thanks a lot Bruno..

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

    HUGE helps. Thank you!

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

    Nice content Man, save my day !

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

    TOP 👌🏽...dependent dropdown in react js 🙏

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

      Thank you ❤️
      Do you want to learn how to do one dependent dropdown? Or learn how to test one dependent dropdown? 😊

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

      @@BrunoAntunesPT I found what I was looking for and I swear to God that your series saved my job in the company I don't know how to thank you ... May God protect you ... THANKS A LOT ....
      th-cam.com/video/6FKSqWmZ5Ec/w-d-xo.html&ab_channel=BrunoAntunes

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

      You put a big big smile on my face 😍 I'm very happy my videos can help ❤️ thank you for your words 😊🙏

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

    Parabéns pela didática cara, um abraço aqui do Brasil.

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

    When I add rest.get("/api/photos", (req, res, ctx) => {}), TypeScript tells me that Photo[] does not satisfy the constraint 'PathParams'. Index signature for type 'string' is missing in type 'Photo[]'. Why can I not annotate the type here?

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

      If you can share your repository, I'll have a look why you are facing that error, but without running code is very hard to help 🙂

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

      @@BrunoAntunesPT I wonder if the signature changed in a newer version. Things seem to be working now if I do rest.get(...)

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

    Awesome video lessons))
    Mr. Bruno, I see you use Windows OS.
    Can you do a comparison video between Windows, Mac, and Linux? Just interested in your opinion))

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

      Thanks you very much 😍😍
      Honestly, I like the 3 🙂 I change quite often between them.
      I recently bought a Mac mini with m1 chip and I'm enjoying it quite a lot 🙂
      Sadly, my windows laptop died and I needed something good and cheap, so I bought this Mac mini which is faster than my old laptop for like 1000 dollars (16gb ram 512gb disk) 🙂
      If you need a computer today I would highly recommend an m1 or m1 pro today 🙂

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

    Good lesson, why do you nest describe blocks? I never saw people do that before, is it possible to do this with no more than single level deep describe blocks? With nested describe blocks tests look chaotic and complex.

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

      I prefer it that way, because it's easier to read and understand. This way tests make more logical sense to me. That being said I'm aware some people might have a different opinion and that's absolutely fine 🙂
      Answering your question, I have good news for you: you don't even need to use describe blocks in tests, they are 100% optional 🙂

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

    voltaste! Boa! :D

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

    Great content and I love your energy. Thanks a lot Bruno 🤙 Can I use a real API endpoint for my handler instead of api directory of next js for example Rick and morty API?

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

    Hey Bruno! I noticed that you use void operator before some function calls. Never seen such pattern before, where can I read about it? Tried to google but haven't found any exact explanation))

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

      That's a eslint rule. It's a very very handy eslint rule to have in your production projects, to check if you catch all the errors from your promises or not 😊
      when you are sure you don't want to catch errors from a specific promise you put void and the rule knows you did that on purpose not because you forgot to handle errors 😊
      github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-floating-promises.md

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

      @@BrunoAntunesPT, appreciate your explanation, thanks!

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

    can use it with getstaticProps or getserversideProps?

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

      Yes, it can
      in fact on apps I use msw also as the mock server, not only for tests but for development, including server and client 🙂

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

    Super confusing. I need something simplified.

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

      This is the easiest way to confidently mock an http call 🙂

  • @76Freeman
    @76Freeman 2 ปีที่แล้ว

    Viva Bruno tudo bem? :D
    First of all, a huge thank you for the great content, I love how you explain the subjects.
    Bruno, I'm trying to get MSW to work with axios interceptors but I can't get it to work properly.
    We've created an IIFE (Immediately Invoked Function Expression)
    So we have defined some defaults:
    export const httpClient = (function () {
    const axiosInstance = axios.create();
    axiosInstance.defaults.baseURL = backendUrl; // localhost
    axiosInstance.defaults.headers.common['Content-Type'] = 'application/json';
    axiosInstance.defaults.withCredentials = true;
    We have a axiosInstance.interceptors.response.use(...) where, in case of an error, we check for the error and we do something, not really important here.
    return {
    backendUrl,
    axiosInstance: axiosInstance,
    get: (path: string, params: any = {}) =>
    axiosInstance
    .get(path, { params, paramsSerializer })
    .then((res) => res.data),
    ... // more http verbs
    }:
    })()
    Then, we have a function to fetch some endpoints
    export const getRoutes = () => httpClient.get('api/routes', { page: -1, size: -1 });
    In our components we are using React Query so we are doing const { data } = useQuery(['getRoutes'], getRoutes);
    I then have a handler for this route
    export const routeHandlers: RequestHandler[] = [
    rest.get('/api/routes', (req, res, ctx) =>
    res(ctx.status(200), ctx.json(routesMock))
    ),
    ];
    Then I've set up the server
    import { routeHandlers } from 'mocks/handlers';
    import { setupServer } from 'msw/node';
    export const server = setupServer(...routeHandlers);
    But this endpoint /api/routes never gets intercepted.
    I'm not sure what I'm doing wrong, I've checked your repo and I'm doing the same thing as you.