Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"InvalidStateError: The object is in invalid state" when mocking rest api #2089

Closed
4 tasks done
Lespoir opened this issue Mar 15, 2024 · 1 comment
Closed
4 tasks done
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@Lespoir
Copy link

Lespoir commented Mar 15, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v18.15.0

Reproduction repository

No Repo

Reproduction steps

Versions:

  • "react-native": "0.71.11"
  • "msw": "2.2.3"
  • "undici": "5.22.0"

Setup:

  • Polyfill file created with:
const { TextDecoder, TextEncoder } = require('node:util')

Object.defineProperties(globalThis, {
    TextDecoder: { value: TextDecoder },
    TextEncoder: { value: TextEncoder },
})

const { Blob } = require('node:buffer')
const { fetch, Headers, FormData, Request, Response } = require('undici')

Object.defineProperties(globalThis, {
    fetch: { value: fetch, writable: true },
    Blob: { value: Blob },
    Headers: { value: Headers },
    FormData: { value: FormData },
    Request: { value: Request },
    Response: { value: Response },
})

Steps:

  • Add a rest handler like
server.use(
    rest.post(`/test`, () =>
      HttpResponse.json({}),
    ),
  );
  • Create a test that checks the mock is called

Current behavior

Getting this error:

detail: Invariant Violation: InvalidStateError: The object is in invalid state.
          at next (/Users/davidavila/Documents/gg/core-mobile/node_modules/@mswjs/interceptors/src/utils/createProxy.ts:81:24)
          at Object.handler.get (/Users/davidavila/Documents/gg/core-mobile/node_modules/@mswjs/interceptors/src/utils/createProxy.ts:86:11)
          at onloadend (/Users/davidavila/Documents/gg/core-mobile/node_modules/axios/lib/adapters/xhr.js:101:17)
          at Timeout.task [as _onTimeout] (/Users/davidavila/Documents/gg/core-mobile/node_modules/jsdom/lib/jsdom/browser/Window.js:520:19)
          at listOnTimeout (node:internal/timers:569:17)
          at processTimers (node:internal/timers:512:7),

Expected behavior

Test should pass

@Lespoir Lespoir added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Mar 15, 2024
@kettanaito
Copy link
Member

Hey, @Lespoir. Thanks for reporting this. Please try out the new version of MSW, I think this has been fixed in the underlying dependency library. If the issue persists, please provide a reproduction repository. I cannot help here without having a code to dive in. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

2 participants