DX Improvement for moving to @apollo/client v3 with bad mocks #8151
Labels
🏓 awaiting-contributor-response
requires input from a contributor
discussion
🧞♂️ enhancement
🔍 investigate
Investigate further
🌹 has-reproduction
🔬 testing-utilities
First off, let me say <3 for all the effort y'all put into to making this library great! The improvements of v3 look awesome :)
This ticket is the result of a few solid days of me tracking down a bug in our code when trying to upgrade.
The tl;dr is that bad code in v2 could silently fail. When upgrading to v3, we get an assertion error that was difficult to debug. I'd like to suggest adding more actionable information and suggestions in the error message.
I know this is edge casey, but let me take you on this journey anyway - and maybe this might help future folks who run into this and google their stack trace.
Status Quo
Apollo versions
We're on 3.x for @apollo/react-{hooks,common,testing}
(The behaviour is different for 4.x, more details below)
Code
Full Minimal Repro: https://glitch.com/edit/#!/educated-lydian-editorial
(you can open the terminal and run
npm test
)Here's the relevant bits:
App.js
test.js
The big problem here is that setting
result.error = true
in the mock is not the right way to test an error state, per the docs.But it's an understandable, easy mistake to make. It looks like it could be correct. And in fact, the test fails as expected, because in @apollo/react-* @ 3.x, this bubbles up to become a Network error, and is caught by the error handling logic in the test as "expected".
full stack trace
So the error gets caught, and the test "works" (renders null) as the dev expects, but for the wrong reasons.
(I notice that on @apollo/react-* @ 4.x, the behaviour changed and we do get a different error that makes the test break. TODO: add stack trace)
@apollo/client v3
When I moved to @apollo/client @ 3.x, the test starts failing with
Error: Uncaught [Invariant Violation: Missing field 'vehicle' in {}]
:full stack trace
full repro: https://glitch.com/edit/#!/orange-coffee-mango
With hindsight, this is "expected" (looks like this was noticed in @apollo/react-* @ 4.x). But when trying to upgrade a pretty large codebase, this was something that took a while to debug.
The assertion error catches a proper userland bug and at least prevents a bad test from passing, so 👍
Recommendation
I would suggest adding more actionable feedback to this error message - something like:
Closing thoughts
Sorry for the long winded issue, but thought you'd appreciate the war story and experience upgrading.
Overall I love the simplicity of everything under @apollo/client and this is a great project. Thanks for all your hard work! <3
Versions
The text was updated successfully, but these errors were encountered: