[英]"Expected signal to be an instanceof AbortSignal? with Jest Fetch Mock
I am testing an rtk query mutation api call.我正在测试 rtk 查询突变 api 调用。 I'm trying to match the mock result to a successful api call but I always get an error: 'Expected signal to be an instanceof AbortSignal'.
我试图将模拟结果与成功的 api 调用相匹配,但我总是收到错误消息:“预期信号是 AbortSignal 的实例”。 I understand that this may be a side effect of jest-fetch-mock but am not sure how to convert the input(?) into a typeof AbortSignal.
我知道这可能是 jest-fetch-mock 的副作用,但我不确定如何将输入(?)转换为 typeof AbortSignal。
test('successful response', async () => {
const wrapper = ({children}) => {
const storeRef = setupApiStore(testApi);
return <Provider store={storeRef.store}>{children}</Provider>;
};
const classState = {
statusCodes: 200,
message: 'Success',
details: {
overallPresent: 3,
overallTotal: 10,
classStrength: [
{className: 'class 1', present: 3, total: 3},
{className: 'class 2', present: 0, total: 7},
],
},
};
const body = {
schoolName: 'school 1',
className: 'class 1',
};
fetchMock.mockResponse(JSON.stringify(classState));
const {result, waitForNextUpdate} = renderHook(
() => useClassMutation(),
{wrapper},
);
const [students, initialResponse] = result.current;
expect(initialResponse.data).toBeUndefined();
expect(initialResponse.isLoading).toBe(false);
act(() => {
students(body);
});
await waitForNextUpdate({timeout: 500});
const loadedResponse = result.current;
console.log('loadedResponse: ', loadedResponse);
expect(loadedResponse).toEqual(paradeState); //error: 'Expected signal to be an instanceof AbortSignal'
});
This is not an RTK Query error - it's an error from the fetch
polyfill you are using.这不是 RTK 查询错误 - 这是您使用的
fetch
polyfill 的错误。
RTK Query is passing an AbortSignal
into that fetch
call - and then that fetch
polyfill complains that AbortSignal
is something incompatible. RTK 查询将
AbortSignal
传递到该fetch
调用中 - 然后fetch
polyfill 抱怨AbortSignal
是不兼容的。
=> if you polyfill global.fetch
, you also have to polyfill global.AbortController
with a compatible implementation. => 如果你
global.fetch
,你还必须使用兼容的实现来global.AbortController
。
How you do that is very different for each implementation of fetch
, so you'll have to research that - but I hope this gives you a direction.对于
fetch
的每个实现,你如何做到这一点是非常不同的,所以你必须研究它 - 但我希望这能给你一个方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.