mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
535 stars 120 forks source link

Intercepted fetch results in the the process never exiting / hangs Mocha tests #491

Closed vassudanagunta closed 1 month ago

vassudanagunta commented 8 months ago

I'm new to mswjs. I tried to use FetchInterceptor in a Mocha test, and the test would always hang indefinitely, with no console output, until I hit CTRL-C in the terminal (SIGINT), at which point all the console output suddenly dumbs and the test reports success or fail and other tests continue. The console output showed two things: Everything ran to completion, and did so immediately based on the timestamps printed in the console log.

To isolate this and eliminate Mocha as a culprit, I created the following script:

import {FetchInterceptor} from '@mswjs/interceptors/fetch'

const interceptor = new FetchInterceptor()
interceptor.apply()

const requestURL = 'https://raw.githubusercontent.com/vassudanagunta/htmlnorm/main/README.md'

console.log('before fetch', new Date().toISOString())

const resp = await fetch(requestURL)
console.log('after fetch', new Date().toISOString())
console.log('fetched', resp)

interceptor.dispose()
console.log('after dispose', new Date().toISOString())

executing node <path to above script> results in everything running as expected all they way to the last console write, but the process doesn't exit. Not until a SIGINT.

Commenting out either interceptor.apply() or const resp = await fetch(requestURL) eliminates the hang. The script is so simple, I can't imagine what I am doing wrong.


Node v21.4.0 @mswjs/interceptors v0.25.13

kettanaito commented 1 month ago

Hi, @vassudanagunta. Thanks for reporting this. 0.25 is quite an old version of Interceptors. Please update to the latest version and see if the issue has been fixed. If it hasn't, create a reproduction repository I can clone and run to reproduce the issue. Thanks.