Skip to content

Crash when trying to mutate request body in FetchBodyUtility.cloneBodyStream #1963

@sbalay

Description

@sbalay

Describe the bug
I wish I could share a detailed explanation of what I'm seeing... but I lack the time and understanding to go deep into what's going on.

I'm setting up msw in my project that runs integration tests with vitests and sets happy-dom as the running environment.

When mocking a POST request, I get the following error

TypeError: Cannot set property body of #<Request> which has only a getter
    at Function.cloneBodyStream (file:///Users/sebastianbalay/projects/stilla-server/node_modules/happy-dom/lib/fetch/utilities/FetchBodyUtility.js:130:32)

To Reproduce
I don't know exactly what's different from my test suite that causes this crash, but all I'm doing is mocking a POST request with MSW.

Feel free to ask follow up question and I'll do my best to answer.

Expected behavior
Not crash

Suggested solution

Looking around the happy-dom codebase, it seems that request objects use symbol to access read-write versions of its attributes, so the fix for this issue is maybe changing https://github.com/capricorn86/happy-dom/blob/v20.0.11/packages/happy-dom/src/fetch/utilities/FetchBodyUtility.ts#L149 with:

// use symbol accessors instead of requestOrResponse.body = stream1;
requestOrResponse[PropertySymbol.body] = stream1;

I applied that patch locally and things seem to be working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions