How to set the handler domain based on environment flag #2061
-
I want to use MSW both during testing and during development. While testing I want msw to hijack requests to localhost:3000/api/ whereas during development, I want msw to hijjack request to staging.example.com/api/ Is there any way to set this up within MSW? If not i'd be if the domain could be set using an API along the lines of:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, @canrozanes. Thanks for the question, it's a good one. You can achieve what you want by encapsulating that URL logic in a helper function. Something like function url(path) {
const domain = env.isTesting ? 'http://localhost/api' : 'http:// staging.example.com/api'
} And then use that function to create URL predicates in request handlers: http.get(url('/user'), resolver)
http.post('url('/cart/:id'), resolver) This is the way I recommend you do it. Note that if your resource paths will remain the same, which they will, I can recommend dropping the domain name from the URL predicate. If your website is deployed at http.get('/api/user', resolver) The |
Beta Was this translation helpful? Give feedback.
Hi, @canrozanes. Thanks for the question, it's a good one.
You can achieve what you want by encapsulating that URL logic in a helper function. Something like
url()
:And then use that function to create URL predicates in request handlers:
This is the way I recommend you do it.
Note that if your resource paths will remain the same, which they will, I can recommend dropping the domain name from the URL predicate. If your website is deployed at
staging.example.com
during the!env.isTesting
scenario, you can t…