mswjs / interceptors

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

fix(xhr): allow setters for non-existing properties #477

Closed kettanaito closed 9 months ago

kettanaito commented 9 months ago

Investigation

This issue is caused by the Axios XHR adapter in Happy DOM only. Axios does:

request.timeout = config.timeout

Where request is already a proxy established by Interceptors. It looks up the setProperty trap on the proxy and since timeout doesn't have a special case, it defaults to invoke().

Calling invoke() to set timeout on XMLHttpRequest proxy returns false because property timeout does not exist on target:

https://github.com/mswjs/interceptors/blob/1f04d100bad190a393a5855fc6cc3a54f6117dfb/src/utils/findPropertySource.ts#L9-L11

The timeout property is not mandatory on XMLHttpRequest, so it's missing from the immediate XHR instance as well as its prototype. We return false from the setter and that causes the following error:

TypeError: 'set' on proxy: trap returned falsish for property 'timeout'

Solution

I think us forbidding setters on unknown properties is a mistake. To fix this, I'm adding a fallback to propertySource to be the immediate target itself is findPropertySource returned null.

kettanaito commented 9 months ago

Released: v0.25.9 πŸŽ‰

This has been released in v0.25.9!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.