Guidance on a dev flow that switches between mocked responses and real ones
See original GitHub issueIs your feature request related to a problem? Please describe. In my practice, I often switch my dev setup between hitting the real API (for correctness testing) and using the MockServiceWorker for its faster and easier to control response.
My current approach in a create-react-app project is this:
if (process.env.NODE_ENV === 'development' && process.env.REACT_APP_MOCK_API === 1) {
require('./__api_mocks__')
}
However, switching between mocked and non-mocked responses requires switching the ENV variable and restarting the webpack process.
Describe the solution you’d like Maybe an interface on localStorage could be exposed to control msw behavior. Possibly something like:
// disable
localStorage.msw = false
// enable
localStorage.msw = undefined | true
// mock only requests matching urls like "products/list" by providing a regex
localStorage.msw = /products\/list/
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
How to Mock API Requests in Front-End Development? - Codit
Let's look at a high-level diagram of MSW's request flow: You'll need to write declarative request handlers to capture your requests, as well ......
Read more >Python Mocking 101: Fake It Before You Make It - Fugue
This post was written by Mike Lin. Welcome to a guide to the basics of mocking in Python. It was born out of...
Read more >Stubbing and Mocking in Java with the Spock Testing ...
Learn how to create true Java unit tests by mocking all external dependencies in your unit tests with the Spock testing framework.
Read more >Mocking is a Code Smell - Medium
One of the biggest complaints I hear about TDD and unit tests is that people struggle with all of the mocking required to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kettanaito Using your code as a base, I added only a couple of small fixes (eg,
worker.stop()
doesn’t return a promise. Also, I wanted to have a way to specify default options).Thanks a lot for your assistance! Hopefully, this is useful for other people too.
My pleasure! True,
stop()
at the moment doesn’t return a Promise. It used to unregister the worker, but now the worker unregisters itself when the last controlled client is closed.stop()
now stops the mocking, removing the client’s id from the worker’s internal list of clients.Glad it helped!