Special request: Can you do a tutorial on unit testing for React + GraphQL (pref Jest, Enzyme) for…
meaty oaker
21

Writing about testing with React and GraphQL is on my TODO list for sure! And Jest + Enzyme is my goto setup :)

But, that article isn’t coming out anytime soon, so let me jot down a few thoughts on testing:

In general, I’d suggest an approach to testing much like the recommended approach with redux, where you test React components separately from GraphQL, since it just wraps your HOC and passes everything into its props.

I’d say right now, testing GraphQL queries with react-apollo is not well defined, and definitely non-obvious when we have queries and mutations that do complicated things like update other queries.

What you can do is mock what you would receive from your server via graphql-tools. This example is an excellent starting point. From there, you can create dummy components to wrap your react-apollo graphql queries and mutations and then check your apollo store and/or dummy component props to make sure the query/mutation does what it’s supposed to do, including all the side-effects. I’m still messing with this to get a consistent flow that I really like — especially with subscriptions, which is a whole other beast.

Does that make sense? Any thoughts appreciated!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.