2

I have an application that is using Apollo client and when I added MSW I am finding that the cache is returning stale requests with empty responses. The MSW team indicated that best practice is when running tests to clear the cache on every request but I want to run my app in mocks mode for manual testing too.

Doing the following when running the app with mocks enabled seems to work but seems counter-intuitive to have to put mock logic like this into the app. Anyone else encounter this?

const [
   fetchUser,
   { loading, error, data, refetch, networkStatus },
 ] = useLazyQuery(QUERY_GET_USER, {
   fetchPolicy: API_MOCKING ? 'network-only' : 'cache-and-network',
   nextFetchPolicy: API_MOCKING ? 'network-only' : 'cache-first',
 });
Gus Higuera
  • 95
  • 1
  • 9

1 Answers1

1

I'm not able to comment on your original question, so I'll make an answer instead to let you know that you can at least override the default fetchPolicy, even in apollo-boost. That might make setting the fetchPolicy to 'network-only' for your tests a bit less painful.

Reference: https://github.com/apollographql/apollo-client/issues/3900#issuecomment-420830472

midanosi
  • 31
  • 2