Mock Service Worker Tutorial | How to Mock Network Requests

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

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

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

    Note: you no longer have to add the 'jest-environment-jsdom-sixteen' test environment with the latest version of CRA/react-scripts.

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

    Great video, thanks for the bit at the end on using it in standard dev. Whats especially good is that the same handlers can be used for dev and test.

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

      Thank you. Yea I agree, it can make local development a lot easier.

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

    Thank you! now it became much clear how to use MSW in test and development

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

      You’re welcome! Happy it helped.

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

    Thanks @Redhwan Nacef , you made it very clear and simple will be very useful for web projects , especially making it as dev environment was awesome, it saves lot of time and effort.

    • @RedhwanNacef
      @RedhwanNacef  4 ปีที่แล้ว

      You’re welcome, thanks for the feedback.

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

    You gave me a new vision ... thanks

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

    Great tutorial, thanks

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

      Thanks, glad you enjoyed it.

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

    Hi
    Nice Video. I have a query , is it possible to expose these API to public way(means can everyone utilize this APIs)

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

      Thanks. Hmm, good question, I don't think there is a way to do it out of the box, however maybe you could reuse the handlers to return responses for a real api?

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

    Thanks for the tutorial! If the service worker is started and stopped within each test section will this add a lot of extra overhead time when running an entire test suite of maybe say a few hundred tests?

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

      You're welcome. I think any overhead will be negligible when testing. Running tests happens within the node environment instead of the browser, so no actual service worker exists, MSW have a node implementation that will mock the network requests. Also, when running tests you would usually start/stop the mock server at the start and end of the entire test run, the only thing that happens after each tests is resetting the handlers. With this in mind I don't think there is really anything that would add much overhead. Hope that makes sense.

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

    Hi Redhwan, where do you mock your database etc in order to mock a proper Restful API with proper business data. I assume that you simply store it locally

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

      Hi, yes that's right I would usually use an in memory database or stub/mock those responses manually.

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

    Thanks man

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

    Like and comment for algorithm.

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

      Thanks! Somehow missed this comment all those month ago…

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

    I tried this in my react unit tests. But server is not listening to rest.post call so instead of making mocked api call its making actual axios api call.

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

      Sounds like something is not set up right. If you are able to share the code with me, I would be happy to take a look.

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

    What VSCode extensions are you using?

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

      I’m using IntelliJ here instead of VSCode

  • @545himanshu
    @545himanshu 2 ปีที่แล้ว

    Can mock Eventsource?

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

      Good question, I am not sure I'm afraid, something to look into. I haven't seen first class support anywhere from MSW but maybe there is a way...

    • @545himanshu
      @545himanshu 2 ปีที่แล้ว

      @@RedhwanNacef i tried to look for that nd Found out npm mock sse but unfortunately it didn't Work, plz if possible try for that

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

    reading documentation, yaay