响应修补
结合原始和模拟响应。
你可以使用一种称为响应修补的技术将原始响应和模拟响应组合在一起。它涉及按原样执行拦截的请求,获取其原始响应,并根据需要对其进行修改。
¥You can combine original and mocked responses using a technique called response patching. It involves performing the intercepted request as-is, getting its original response, and modifying it as you want.
使用 msw
中的 bypass()
函数执行任何 Fetch API Request
,绕过任何其他匹配的请求处理程序,并防止在使用 bypass()
时由处理程序导致的无限循环:
¥Use the bypass()
function from msw
to perform any Fetch API Request
, bypassing any otherwise matching request handlers and also preventing an infinite loop caused by the handler where you’re using bypass()
:
import { http, HttpResponse, bypass } from 'msw'
http.get('/resource', async ({ request }) => {
// Get the original JSON response from the server.
const originalData = await bypass(request).then((response) => response.json())
return HttpResponse.json({
// Combine the original data with the mocked data.
...originalData,
id: 'mocked-id',
})
})
bypass
API reference for the `bypass` function.