In order to use GraphQL Testing Library and MSW with Jest, missing Node.js globals must be polyfilled in your environment.
Create a jest.polyfills.js file with the following contents:
This file mostly contains the Jest polyfills recommended by MSW, with two additions: the version above includes ReadableStream which is needed for incremental delivery features, and a polyfill for Symbol.dispose which is not available in Jest in versions before 30.0.0-alpha.3.
Then, set the setupFiles option in jest.config.js to point to your jest.polyfills.js:
Finally, install @graphql-tools/jest-transform as a dev dependency and configure Jest to transform .gql/.graphql files, since your GraphQL API’s schema is needed to configure the Mock Service Worker request handler this library generates.
Here are the relevant parts of your final jest.config.js:
With Vitest
No polyfills are needed in Vitest. In order to transform .gql/.graphql files, install vite-plugin-graphql-loader as a dev dependency and configure it in your vitest.config.ts.