-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to access response headers when using axios 1.x with jest #5017
Comments
I am experiencing the same issue with axios version 1.0.0. In plain node (ts-node) headers are returned correctly, e.g. this code returns correctly: const result = await axios.get("https://github.com")
console.log(result.headers.server) the output is however in jest, this code fails: describe("Testing axios against external service", () => {
it("should return headers ", async () => {
const result = await axios.get("https://github.com")
expect(result.status).toBe(200)
expect(result.headers.server).toBe("GitHub.com")
})
}) The In my investigation/debugging and tracing it boils down to this function: Lines 129 to 136 in 7a06a36
Specifically this line: Line 135 in 7a06a36
The response.headers in normal node are correctly identified as plain object, however in jest it does not identify them. It seems that in jest the prototype is different from the standard Object prototype, although I cannot find where they differ. |
And I am not sure what is exactly the purpose of comparing the prototypes, so I do not feel confident enough to propose a patch here. |
It's fixed already with version 1.1.2. |
Describe the bug
When using axios to send requests within jest tests, it's not possible to see the response.headers anymore.
This was working fine with axios version 0.x so far.
To Reproduce
I've prepared a small example to show that it it is not working as expected.
On console, I can see the following log:
Expected behavior
I'd expect that the response headers are also accessible within the jest environment.
Solution proposal
I've narrowed it down that the response headers are normalized here:
axios/lib/adapters/http.js
Lines 427 to 433 in 738fa63
Within the setter of the AxiosHeaders there's a strict check whether the headers are an object:
axios/lib/core/AxiosHeaders.js
Line 114 in bdf493c
Somehow by jest magic, the
header.prototype
is notObject.prototype
(even though it looks like it would be), hence the check returns false, even though it's an object.In my naive opinion, the strict check is not required.
Another option would be to restore the previous behavior. With version 0.27.2, there was no normalization using AxiosHeaders:
axios/lib/adapters/http.js
Lines 288 to 294 in bc733fe
Environment
Additional context/Screenshots
I've also created a small sample repository to reproduce the problem even easier:
https://github.com/johannes-vogel/axios-jest-issue
The text was updated successfully, but these errors were encountered: