Skip to content

fix: clone response before mutating it as of ISSUE-103#105

Open
RobinBertilsson wants to merge 1 commit intovalendres:mainfrom
RobinBertilsson:main
Open

fix: clone response before mutating it as of ISSUE-103#105
RobinBertilsson wants to merge 1 commit intovalendres:mainfrom
RobinBertilsson:main

Conversation

@RobinBertilsson
Copy link
Copy Markdown

As mentioned in ISSUE-103.

an error is happening when same API endpoint is triggered multiple times during test execution. Seems like error comes from onMockedResponse method, comparing it with same method from msw we can see that response is cloned before usage. When I've added response.clone() problem is gone.

@RobinBertilsson
Copy link
Copy Markdown
Author

@valendres could you please have a look? 🙏

headers: rawHeaders,
body: rawBody,
}) => {
onMockedResponse: async (response) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's much better to use a higher-level getResponse() instead of handleRequest(). Any history as to why handleRequest is used instead? Is it to tap into resolutionContext.baseUrl?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure i'm following... Where do you mean i would use handleRequest?
The only difference in this PR is that i accept the request parameter provided by onMockedResponse, which i then use to clone the response, before mutating it. The reason for the clone is because the stream is "locked" as per this error message.

ReadableStream is locked

Copy link
Copy Markdown

@kettanaito kettanaito Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment isn't related to your changes. If you scroll a few lines above your change, you see the usage of handleRequest. Funneling the mocked response from onMockedResponse to Playwright isn't the best place so I'm suggesting using getResponse() instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants