axios 0.19.1 breaks jest tests that use nock
See original GitHub issueDescribe the bug After upgrading axios from 0.19.0 to 0.19.1 all tests that use nock got broken. They now produce the following error:
Error: Cross origin http://localhost forbidden
To Reproduce Repo with minimal test case that reproduces the issue: https://github.com/EvgenyOrekhov/js-framework/tree/axios-bug.
Note that previous commit (which has axios 0.19.0) works fine, the test passes.
import axios from "axios";
import nock from "nock";
it("makes HTTP requests", async done => {
const scope = nock("https://example.com/foo/")
.get("/bar")
.reply(200, { foo: "bar" });
await axios.get("https://example.com/foo/bar");
scope.done();
done();
});
Expected behavior The test should pass.
Environment:
- Axios Version: 0.19.1
- OS: Ubuntu 19.10
- Browser: n/a
- Browser Version: n/a
- Additional Library Versions: nock 11.7.2
Additional context/Screenshots Jest error with stack trace:
FAIL src/http.test.js
✕ makes HTTP requests (34ms)
● makes HTTP requests
Network Error
at createError (node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (node_modules/axios/lib/adapters/xhr.js:83:14)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
at XMLHttpRequestEventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
at XMLHttpRequestEventTargetImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
at XMLHttpRequest.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:132:7)
at dispatchError (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:62:3)
at Object.validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:77:5)
at receiveResponse (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:847:21)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:679:38)
at Request.onRequestResponse (node_modules/request/request.js:1066:10)
at respond (node_modules/nock/lib/playback_interceptor.js:299:11)
at respondUsingInterceptor (node_modules/nock/lib/playback_interceptor.js:341:7)
at node_modules/nock/lib/playback_interceptor.js:281:7
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Cross origin http://localhost forbidden
at dispatchError (/home/user/my-app/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
at Object.validCORSHeaders (/home/user/my-app/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:77:5)
at receiveResponse (/home/user/my-app/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:847:21)
at Request.<anonymous> (/home/user/my-app/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:679:38)
at Request.emit (events.js:321:20)
at Request.onRequestResponse (/home/user/my-app/node_modules/request/request.js:1066:10)
at OverriddenClientRequest.emit (events.js:321:20)
at respond (/home/user/my-app/node_modules/nock/lib/playback_interceptor.js:299:11)
at respondUsingInterceptor (/home/user/my-app/node_modules/nock/lib/playback_interceptor.js:341:7)
at /home/user/my-app/node_modules/nock/lib/playback_interceptor.js:281:7 undefined
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:15 (1 by maintainers)
Top Results From Across the Web
Controlling axios HTTP error handling and test it using nock + ...
Conclusion. In this article I have shown how to control of the axios HTTP client library's error emitting mechanism, and how to set...
Read more >Jest and HTTP mocking with nock - unit testing - Stack Overflow
Basically what happens in 'real life' is that I type a city into an input into a frontend, that city gets posted to...
Read more >NOTICE.pdf - NetApp Cloud Central
This product does not necessarily use all the software components referred to ... @jest/test-sequencer 25.5.3. MIT License ... axios 0.19.1. MIT License.
Read more >21.07 - Teradici
... License @jest/test-sequencer 26.6.3 : MIT License @jest/transform 26.6.2 ... Permission to use, copy, modify, and/or distribute this software for any ...
Read more >jest-environment-nock-axios - npm
Mock all network requests in tests using nock. Purpose. This environment ensures that no unmocked network requests are made (by calling nock's ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here’s how you can solve this. (If you don’t test for CORS).
Have
nock
includeAccess-Control-Allow-Origin
in the response.Or use it with nocks’s default reply headers option
Hmm… switching the test environment to “node”, does not help when you test a react app.
jsdom
is needed for the tests to work that rely on browser environment.