Add a Single Source of Truth for Fixtures in MSW

Share this video with your friends

Send Tweet

As you add more request handlers in Mock Service Worker, introducing a source of truth for the static data can help reduce the clutter and keep the network description focus on behavior instead of repeating fixtures. In this lesson, we will refactor our code by moving the static data outside and reusing it across existing request handlers.

Kushal Mahajan
~ a year ago

It would have been helpful to have movies stored somewhere in content folder. There is not reference to the API as well in this or previous two videos. I had to hardcode the movie :(

Artem Zakharchenkoinstructor
~ a year ago

Hey, Kushal. You can store the static fixtures wherever you prefer. This is just an illustration of the concept that keeping them in a single place and then referencing them in different request handlers is beneficial to reduce repetition. You can even use libraries like Faker to generate content on runtime. Imagination is your only limit! Well, imagination and JavaScript ;)