Does it allow for mocking on demand? #955
-
I want to mock a POST request on demand (when test presses a button) is it possible here or should I use other library? |
Beta Was this translation helpful? Give feedback.
Answered by
kettanaito
Oct 29, 2021
Replies: 1 comment 1 reply
-
Hey, @adammo94. You can control whether mocking is enabled on runtime by starting or stopping the worker whenever you need. Here's how I'd implement an on-demand mocking as you described: // mocks/browser.js
import { setupWorker } from 'msw'
import { handlers } from './your-handlers'
// Create the worker instance but don't start it yet.
// This means that the mocking is configured but not active.
export const worker = setupWorker(...handlers) // ui.js
import { worker } from './mocks/browser'
btn.addEventListener('click', () => {
// Start the worker to enable requests interception.
worker.start()
})
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kettanaito
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, @adammo94.
You can control whether mocking is enabled on runtime by starting or stopping the worker whenever you need.
Here's how I'd implement an on-demand mocking as you described: