'Setting headers for JEST integration testing on Apollo
Hi i'm facing an error in my code, this is the code. I am not able to set the headers inside executeOperation function of Apollo as i need accessToken to run next mutation or a query. loginToken is being set and i can retrieve it's value . But when i set headers before query "getClientConfiguration" it gives me unauthorized response because of header.
const { createServerWithSchema } = require('../../gql/server')
let loginToken;
describe('Space test suite', () => {
it('returns login token', async () => {
const testServer = await createServerWithSchema()
const result = await testServer.executeOperation({
query: `
query loginFromEmail{
loginFromEmail(
email:"[email protected]"
password:"Password1"
)
{
accessToken
}
}
`
})
const { accessToken } = result.data.loginFromEmail
loginToken = accessToken
expect(result.errors).toEqual(undefined)
})
it('returns clientConfiguration', async () => {
const testServer = await createServerWithSchema()
const result = await testServer.executeOperation({
http: {
headers : {
"Authorization": `Bearer ${loginToken}`
}
},
query: 'query getClientConfiguration { getClientConfiguration { GOOGLE_MAPS_API_KEY } }',
})
console.log("result", result)
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
