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.

Mocking API.graphql(...).subscribe(...) using Jest

See original GitHub issue

Which Category is your question related to? API, in particular graphql

What AWS Services are you utilizing? AppSync

Provide additional details e.g. code snippets I need to have 100% coverage in my app (its a react app that uses amplify-cli, which in turn uses amplify-js, and I need some guidance into mocking the API subscriptions. This is a piece of code I need to mock in order to test (I am using jest, obviously anything else would work if there are other tools you guys know):

API.graphql(
    graphqlOperation(subscriptions.onCreateItem, { owner: username })
  ).subscribe({
    next: (item) => {
      // Do something with the code, irrelevant for the purposes of this question.
    }
  })

I’ve looked at https://www.apollographql.com/docs/graphql-tools/mocking.html, plus google searched, looked into this repo at the tests that you guys have and also into amplify-cli, but I can’t seem to get it working. If you have any code examples that would be great, I don’t need hand holding to mock the above, just a good example or two that I can take inspiration from.

You guys have a test from subscriptions, but its being skipped (it’s https://github.com/aws-amplify/amplify-js/blob/master/packages/api/__tests__/API-test.ts#L268 and I couldn’t get it working either - I’m getting a GraphQL Network error)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
elchicofrommocommented, Aug 28, 2020

Any movement on this? I just spent yesterday beating my head against this. I opened up another defect WRT testing and Amplify. I kind of have a love/hate relationship with the stack at this point.

0reactions
github-actions[bot]commented, Jul 23, 2022

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React mock Graphql subscription - Stack Overflow
You need to mock the return value of API.graphql as an Observable that come from reactive programming. Then, you can use .subscribe method....
Read more >
Mock GraphQL and REST in Storybook and Jest with MSW
Walk through how to mock GraphQL or REST responses at network level, and share mocks between Storybook and Jest tests…
Read more >
Mocking - Apollo GraphQL Docs
Mocking enables Apollo Server to return simulated data for GraphQL operations based on your server's schema. The strongly-typed nature of a GraphQL API...
Read more >
Mock GraphQL Subscriptions with Laika — the only Apollo ...
Laika to the rescue · have a familiar, typed API, and be as close to jest 's mock function as possible · enable...
Read more >
Mocking and Testing GraphQL in React - Telerik
Or how about wanting to test your components without making real GraphQL requests to the server? With Mocks, both are possible! Mocks allow...
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