Documents / @faasjs/react / setMock
Function: setMock()
setMock(
handler):void
Set the global mock handler used by all FaasBrowserClient instances.
When a mock handler is set, every FaasBrowserClient.action call will
route through the mock after beforeRequest runs and before any native
fetch or custom request implementation is called. The mock is process
global, so it affects every registered React/browser client until cleared.
Parameters
handler
Response<any> | MockHandler | ResponseProps | null | undefined
A mock function that receives (action, params, options) and returns a
response shape, a Response, an Error, or nothing. Static
response shapes and Response instances are also accepted. Pass null or
undefined to disable mocking.
Returns
void
Example
import { setMock, Response } from '@faasjs/react'
// Mock all requests with a static response
setMock({ data: { name: 'test' } })
// Mock with a handler function
setMock(async (action, params) => {
if (action === 'posts/get') {
return { data: { title: 'Hello' } }
}
return new Error('Not found')
})
// Disable mocking
setMock(null)