Graphql Variable Validation with Jest #501
-
We are wanting to put assertions on variables passed to a graphql query. On the example below, we want to make sure variables are called specifically with certain values.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey, @birdwell. MSW doesn't encourage assertion on request data, as it may often be asserted by UI instead. The input data that we sent to the server is often directly or indirectly present in the response that we use to render the UI. If that's possible, strongly consider basing your assertion on the UI that gets rendered by your query response. At the same time, we understand there may be cases when the recommendation above is not fully applicable. You can still issue regular assertions within the response resolver: graphql.query<SaveContent, SaveContentVariables>('SaveContent', (req, res, ctx) => {
expect(req.variables).toEqual({ id: 'abc-123' })
return res(
ctx.data({
saveContent: {
success: true,
id: req.variables.id,
errors: [],
},
})
);
}) Assertion libraries, like jest's |
Beta Was this translation helpful? Give feedback.
Hey, @birdwell.
MSW doesn't encourage assertion on request data, as it may often be asserted by UI instead. The input data that we sent to the server is often directly or indirectly present in the response that we use to render the UI. If that's possible, strongly consider basing your assertion on the UI that gets rendered by your query response.
At the same time, we understand there may be cases when the recommendation above is not fully applicable. You can still issue regular assertions within the response resolver: