xhr.upload.progress not called when using "passthrough()" #1937
-
| 
         I am using AXIOS to upload a file to my backend. I configured a callback to be called for the onProgress  const formData = new FormData();
   formData.append("file", file.files[0]);
   formData.append("json",  JSON.stringify({'test':123}));
   axios.post('https://httpbin.org/post', formData, {
     headers: {
         'Content-Type': 'multipart/form-data'
     },
     onUploadProgress: (e)=>{
         console.info('inside axios on progress',e, e.progress)
     }
 })when msw is disabled, the 'onUploadProgress' callback is invoked and I see the messages: but when I enable MSW, the callback is not invoked. is there any plan to have the xhr.upload.progress event supported in MSW? edit:  | 
  
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
| 
         Hi, @Ahmad-Alzamer. Thanks for reporting this. Could you please update your post to include your request handler for this request? 
 The upload event in Axios is meant to be supported in MSW as-is. We have an Axios upload request test but there isn't a case that uses  
 Not sure I follow. The  Lines 15 to 23 in ea28bd9 This instruction later gets interpreted by the library to not produce any mocked response. There's no request cloning as far as I'm concerned but I may be missing something. Do you have some debugging info that made you conclude the request cloning may be causing this?  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Having kinda same problem with passing through a request as-is. Versions of used libraries:  | 
  
Beta Was this translation helpful? Give feedback.
-
        SolutionFor anyone encountering this, here's how to fix this. You have to use the  Here's an example of how to use Interceptors to resolve your existing request handlers against an XHR interceptor that does support the upload progress: import { getResponse } from 'msw'
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
import { handlers } from './your/handlers/here'
const interceptor = new XMLHttpRequestInterceptor()
interceptor.on('request', ({ request, controller }) => {
  const response = await getResponse(handlers, request)
  if (response) controller.respondWith(response)
})
interceptor.apply()Use this alongside the   | 
  
Beta Was this translation helpful? Give feedback.


Solution
For anyone encountering this, here's how to fix this. You have to use the
XMLHttpRequestInterceptorfrom@mswjs/interceptorsmanually to intercept XHR requests and supportuploadthere. The Service Worker API coerces all intercepted requests to Fetch API requests, and those do not have a concept of upload progress.Here's an example of how to use Interceptors to resolve your existing request handlers against an XHR interceptor that does support the upload progress: