0

so I am using MSW to test my GQL and components, which means mocking Apollo will fail the tests. I want to test that the useQuery function is being called with the correct fetch-policy but it seems virtually impossible without having to mock Apollo within the test file.

MSW cannot test this directly, it seems, as its an Apollo feature. How would I manage test this variable is correct within a test file that uses MSW?

narliecholler
  • 430
  • 5
  • 18

1 Answers1

0

MSW doesn't provide the means to assert the fetchPolicy of your GraphQL client because that property is specific to the said client. To my best knowledge, the fetchPolicy from Apollo doesn't directly translate to any Request properties. It remains an implementation detail of the GraphQL client and controls how it manages re-fetches internally.

I highly recommend against testing the fetchPolicy for two reasons:

  1. This is an implementation detail of your request client. This also means that folks at Apollo have tested it rather well so you wouldn't have to.
  2. It is also your app's implementation detail. The user of your software doesn't even know if you're using Apollo or a specific fetchPolicy set in there. Neither should your tests.

Now, that doesn't mean not testing the behavior bound to that property. To correctly test it, run your application in the actual browser and perform user-based operations that would illustrate a particular fetchPolicy value in action (e.g. that the cached data is displayed first and than is re-fetched). Forget about fetch policies and request clients, focus on what behavior the user would see.

kettanaito
  • 974
  • 5
  • 12