Apollo Server Integration testing documentation shows how to test simple queries using createTestClient
:
const { query } = createTestClient(server);
const res = await query({ query: GET_LAUNCH, variables: { id: 1 } });
In the attempt to test a query which requires an authorization token, I have tried passing the jwtToken
as an extra field to the object passed to query like below:
const res = await query({
query: MY_QUERY,
http: { headers: { authorization: `Bearer ${jwtToken}` } },
});
Unfortunately this doesn't work. When trying to run it I get the following response:
{
"http": {
"headers": {}
},
"errors": [{
"message": "must authenticate",
"locations": [{
"line": 2,
"column": 3
}],
"path": ["myQuery"],
"extensions": {
"code": "UNAUTHENTICATED"
}
}],
"data": {
"myQuery": null
}
}
Any idea how to properly test queries which require an authorization token?
Using:
"devDependencies": {
"jest": "^23.6.0",
"apollo-server-testing": "^2.4.8"
}