question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: GraphQL queries are not intercepted in Jest unit test

See original GitHub issue

Issue

I want to use msw in Jest unit tests only. However, GraphQL queries are not being intercepted. Our stack is React + Jest + Apollo GraphQL.

I have already looked at https://github.com/mswjs/examples/tree/master/examples/graphql-react-apollo which is very helpful. Thanks for that.

Environment

  • msw: 0.19.3
  • nodejs: v12.14.0
  • npm: 6.13.4
  • Browser: Chrome 83.0.4103.97

I have 2 questions.

1. Do I need any extra setup to make msw work in node (Jest unit tests)?

I believe this command is required to make msw work in browser for development. Do I need to do something similar for node?

$ npx msw init <PUBLIC_DIR>

2. Is there any config needed for different url end point?

Our end point is /prism instead of /graphql. Do I need to change any config to make it work besides setting up apollo client?

Note: I have also tried changing our api url to /graphql but it still doesn’t work.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, May 7, 2021

MSW doesn’t currently support batched GraphQL queries. See the support progress in #513.

1reaction
kettanaitocommented, Jul 22, 2020

That’s a good comment. MSW is the replacement for jest-fetch-mock. Using both doesn’t guarantee any reliability and doesn’t make much sense. Thanks for highlighting that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

msw: graphql operation doesn't get intercepted - Stack Overflow
Using MWS, I'm trying to mock the graphql operation testRequest ... the console.log in the graphql.query('testRequest', (_, res, ctx)=> {.
Read more >
Testing React components - Apollo GraphQL Docs
This article describes best practices for testing React components that use Apollo Client. The examples below use Jest and React Testing Library, ...
Read more >
Write tests with confidence - React, GraphQL and MSW
Here we intercept the GraphQL query and send custom responses based on the query variables. Here, we import graphql from msw and, we...
Read more >
Testing | RedwoodJS Docs
In Redwood that's Jest, and it's known as a test runner; The amount of code you ... Redwood just intercepts the GraphQL query...
Read more >
Writing Tests for Apollo Client in React - Robin Wieruch
The tutorial builds up on a mocked GraphQL API enabling you to write tests for your Apollo Client queries and mutations in React....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found