I have a mock service worker (MSW), which intercepts any axios rest api request and mocks it.
I have one specific api request to upload a file, it also has progress event, which tell me about the progress of the file being uploaded.
apiClient.doPut(
`/uploadFile/files`,
formData,
{
timeout: 35000,
headers: {
"Content-Type": "multipart/form-data",
},
onUploadProgress: (progressEvent) => {
const progress: number = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
console.log("progress");
setProgressMap((prev) => new Map(prev.set(fileName, progress)));
},
}).then((res) => {
console.log("Here");
return res.data;
}).catch((error) => {
console.log("error);
});
This code works fine. but when I run the testcases, MSW is unable to intercept this request and the catch block gets executed.
When I remove the onUploadProgress
from the configuration MSW is able to mock the request correctly.
How can I test or mock the onUploadProgress
for this particular API using MSW, Jest or React testing library?