The Do’s and Don’ts of Testing Apollo in React

Unit testing versus Integration testing

Do not mock individual functions 🚫

Do mock at the service worker level ✅

https://mswjs.io/docs/
  • We don’t have to mock any files, libraries or functions. i.e. More of our code gets tested!
  • We can test the Apollo configuration that could impact the output such as caching or type policies
  • Our tests are no longer coupled to Apollo — We could swap out our fetching library or use fetch() directly

Do not define another client for tests 🚫

Do not hard code your mocks 🚫

Do rely on the GraphQL type system ✅

Do Test Driven Development ✅

Conclusion

--

--

--

Tech Lead at Payble. Sydney, Australia. We’re hiring https://payble.com.au

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

React useState

Get META From firebase in Angular for Crawlers like facebook and twitter and for SEO

How a simple log line can stop your business

Mapping Out Information with React

React Native Unit Testing with Detox

Tracing ‘window’ as a Chrome Extension

3 Easy Steps to Set Up React Native with TypeScript, Jest and Enzyme

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Hannigan

Adam Hannigan

Tech Lead at Payble. Sydney, Australia. We’re hiring https://payble.com.au

More from Medium

React local development and testing mocking with msw and mswjs/data

Request and cache data in React like a pro

Polling simplified, with React Query (useQuery)

How To Write An Integration Test With Jest/RTL