Writing tests for functions that user header()
Answered
Kerry Blue Terrier posted this in #help-forum
Kerry Blue TerrierOP
Hello - I'm trying to write a test for a helper function I'm using in the app router on the server side.
The function checks headers for authentication, to get the headers it calls
Is there a standard way to set the header values for use in testing?
The function checks headers for authentication, to get the headers it calls
headers() from import { headers } from 'next/headers'. When I run jest I get the error: Invariant: Method expects to have requestAsyncStorage, none available
Is there a standard way to set the header values for use in testing?
Answered by Kerry Blue Terrier
Going with this for now:
jest.mock('next/headers', () => {
return {
headers: () => {
return {
get: (name: string) => {
if (name === 'authorization') {
return mockAuthenticationHeader
}
return null
},
}
},
}
})2 Replies
Kerry Blue TerrierOP
Going with this for now:
jest.mock('next/headers', () => {
return {
headers: () => {
return {
get: (name: string) => {
if (name === 'authorization') {
return mockAuthenticationHeader
}
return null
},
}
},
}
})Answer
Kerry Blue TerrierOP
mockAuthenticationHeader is defined outside of this so it can be manipulated per test