valendres / playwright-msw

A Mock Service Worker API for Playwright
MIT License
168 stars 24 forks source link

ReadableStream is locked #103

Open mozolevsky opened 1 month ago

mozolevsky commented 1 month ago

Hey, 👋 running my tests I've faced such issue

TypeError: Invalid state: ReadableStream is locked
at handleRequest (/node_modules/msw/src/core/utils/handleRequest.ts:125:25) 

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

// lib/handler.js
     onMockedResponse: response =>
                    __awaiter(void 0, void 0, void 0, function* () {
                        const { status, headers: rawHeaders, body: rawBody } = response.clone(); < ---- fix here
                        var _b;
                        const contentType =
                            (_b = rawHeaders.get('content-type')) !== null && _b !== void 0 ? _b : undefined;
                        const headers = (0, utils_1.objectifyHeaders)(rawHeaders);

                        const body = yield (0, utils_1.readableStreamToBuffer)(contentType, rawBody);

                        return route.fulfill({
                            status,
                            body,
                            contentType,
                            headers,
                        });
                    }),

Does it make sense from your point of view? Thx.

RobinBertilsson commented 2 weeks ago

@mozolevsky, i'm facing the same issue, submitted a PR for it.