Closed m-tartari closed 1 month ago
Hi, @m-tartari. Thanks for reporting this.
Are you per chance forgetting to push the mocks
directory? Neither of the reproduction repos have it, and there's only one test (for the button).
Please include the reproduction steps or update those repos. Thanks.
Hi @kettanaito The repo is as intended. It contains the smallest codebase within which i could show the issue. The mock handlers are written directly inside tests (src/components/DeployButton.test.tsx#L30-L32. and L48-L54).
It's simple a button that, once clicked, sends a couple of custom props to a back-end with a rest api call. It has a suite of 2 identical tests (they only differ by having sync or async mock handler). Currently one is passing and one failing with a TypeError: Body is unusable
when accessing the request payload using Life-Cycle events (in src/utils/testUtils.ts#L29). I would expect the same behavior for both.
For reproduction npm ci && npm run test
will trigger the error.
Hi @kettanaito The repo is as intended. It contains the smallest codebase within which i could show the issue. The mock handlers are written directly inside tests (src/components/DeployButton.test.tsx#L30-L32. and L48-L54.
It's simple a button that, once clicked, sends a couple of custom props to a back-end with a rest api call. It has a suite of 2 identical tests (they only differ by having sync or async mock handler). Currently one is passing and one failing with a
TypeError: Body is unusable
when accessing the request payload using Life-Cycle events (in src/utils/testUtils.ts#L29). I would expect the same behavior for both.For reproduction
npm ci && npm run test
will trigger the error.
I think the issue here is that the same Request
instance is here:
and
a Request.prototype.json()
can only be called on an instance once. You can request.clone().json()
instead in both places to allow it to be read in each.
You could check the payload in the resolver - https://github.com/m-tartari/msw_lifecicle_body_unusable/blob/45318f0470441ee021ac1440a8d0295d1bc508d5/src/components/DeployButton.test.tsx#L49-L53 instead of doing this in the event infrastructure
Thank you very much for the explanation and fix. It solved the issue in both the test code and main project!
Prerequisites
Environment check
msw
versionNode.js version
v20.12.1
Reproduction repository
Reproduction steps
npm ci npm run test
Current behavior
I'm migrating a large code-base from MSW v1.3.4 to v2.4.11. For some test I'm required to ensure the correctness of the request payload. When I try to inspect the content of the payload for with async request handlers I get a
TypeError: Body is unusable
npm run test output
``` ➜ workspace git:(main) ✗ npm run test > msw_test@v1.0.0 test > vitest RUN v2.1.2 /project/workspace stderr | src/components/DeployButton.test.tsx > The Deploy Button > async Error parsing request: TypeError: Body is unusable at specConsumeBody (node:internal/deps/undici/undici:5549:15) at Request.json (node:internal/deps/undici/undici:5451:18) at _Emitter.Currently we are using:
Expected behavior
Both test succeed.