Skip to content
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

0.19.0 no longer works with xhr-mock #2195

Closed
rybon opened this issue May 31, 2019 · 13 comments · Fixed by #2201 or fed135/soixa#13
Closed

0.19.0 no longer works with xhr-mock #2195

rybon opened this issue May 31, 2019 · 13 comments · Fixed by #2201 or fed135/soixa#13
Assignees

Comments

@rybon
Copy link

rybon commented May 31, 2019

Describe the bug
Running Jest unit tests with xhr-mock https://github.com/jameslnewell/xhr-mock fail. Reverting to 0.18.0 fixed it.

@jmelvin-r7
Copy link

Also started seeing the same problem with sinon.js fakeServer in unit tests.

@jasonsaayman
Copy link
Member

jasonsaayman commented Jun 1, 2019

@rybon and @jmelvin-r7 could you create a runkit example for your use case and post it here, that will help a lot with debugging. In addition, can you let me know if there are any POST requests that include params? If so then your issue is probably explained by #2190, and will be mended once #2196 gets merged.

@jmelvin-r7
Copy link

jmelvin-r7 commented Jun 3, 2019

@jasonsaayman - I'm not able to get the tests to run as expected on runkit, but here's the test file that I know works with axios v0.18.1

api.test.js

const sinon = require("sinon");
const axios = require("axios");

const apiRequest = async () => {
  const response = await axios.get("/api/someEndpoint");

  return response.data;
};

test("it should work but does not", async done => {
  const server = sinon.fakeServer.create({
    autoRespond: true
  });

  const responseData = {
    success: true
  };

  server.respondWith([
    200,
    { "Content-Type": "application/json" },
    JSON.stringify(responseData)
  ]);

  const data = await apiRequest();

  expect(server.requests[0].method).toBe("GET");
  expect(server.requests[0].url).toBe("/api/someEndpoint");

  server.restore();
  done();
});

package.json

  "name": "axios-test-fail",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "axios": "0.18.1",
    "jest": "^24.8.0",
    "sinon": "^7.3.2"
  },
  "scripts": {
    "test": "jest"
  }
}

Bump the axios version to 0.19.0 and the test will fail.

@jmelvin-r7
Copy link

@jasonsaayman - I've got a fix for it, really straightforward. The issue was introduced here: 0b3db5d

Part of the change in that commit was the order of the if/else blocks. If those are reversed, the tests pass, and the browser environment still appears to work fine. Unfortunately, I'm unable to push a branch as I'm not considered a collaborator, I believe.

@favna
Copy link

favna commented Jun 4, 2019

Over here upgrading to Axios v0.19.0 doesn't necessarily cause failing tests however it does end up logging a lot of UnhandledPromiseRejection warnings as seen below. Downgrading back to v0.18.0 resolves this issue.

PASS src/__tests__/App.test.tsx
(node:17002) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (~/workspace/app/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:223:79)
    at WindowLocationService.getLocation (~/workspace/app/src/utils/WindowLocationService.ts:30:19)
    at WindowLocationService.get origin [as origin] (~/workspace/app/src/utils/WindowLocationService.ts:10:17)
    at Oauth2ImplicitFlow.redirectToAuthorize (~/workspace/app/src/store/oauth2/oauth2ImplicitFlow.ts:115:59)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:17002) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:17002) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PASS src/__tests__/components/presentational/Navigation.test.tsx
PASS src/__tests__/store/oauth2/StateProvider.test.ts
PASS src/__tests__/config/store.test.tsx
PASS src/__tests__/components/presentational/ModalContent.test.tsx
PASS src/__tests__/config/fontawesome.test.ts
PASS src/__tests__/store/app/appReducer.test.ts
PASS src/__tests__/components/presentational/Logo.test.tsx
PASS src/__tests__/components/presentational/Bar.test.tsx
PASS src/__tests__/store/user/userReducer.test.ts
PASS src/__tests__/store/oauth2/oauth2ImplicitFlow.test.ts
(node:17001) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (~/workspace/app/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:223:79)
    at WindowLocationService.getLocation (~/workspace/app/src/utils/WindowLocationService.ts:30:19)
    at WindowLocationService.get origin [as origin] (~/workspace/app/src/utils/WindowLocationService.ts:10:17)
    at Oauth2ImplicitFlow.redirectToAuthorize (~/workspace/app/src/store/oauth2/oauth2ImplicitFlow.ts:115:59)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:17001) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:17001) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:17001) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (~/workspace/app/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:223:79)
    at WindowLocationService.getLocation (~/workspace/app/src/utils/WindowLocationService.ts:30:19)
    at WindowLocationService.get origin [as origin] (~/workspace/app/src/utils/WindowLocationService.ts:10:17)
    at Oauth2ImplicitFlow.redirectToAuthorize (~/workspace/app/src/store/oauth2/oauth2ImplicitFlow.ts:115:59)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:17001) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:17001) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (~/workspace/app/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:223:79)
    at WindowLocationService.getLocation (~/workspace/app/src/utils/WindowLocationService.ts:30:19)
    at WindowLocationService.get origin [as origin] (~/workspace/app/src/utils/WindowLocationService.ts:10:17)
    at Oauth2ImplicitFlow.redirectToAuthorize (~/workspace/app/src/store/oauth2/oauth2ImplicitFlow.ts:115:59)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:17001) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)

  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "TypeError: Cannot read property 'body' of null
        at getActiveElement (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:5072:16)
        at getActiveElementDeep (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:5353:17)
        at getSelectionInformation (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:5383:21)
        at prepareForCommit (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:8720:26)
        at commitRoot (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:18872:3)
        at ~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:20418:5
        at Object.unstable_runWithPriority (~/workspace/app/node_modules/scheduler/cjs/scheduler.development.js:255:12)
        at completeRoot (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:20417:13)
        at performWorkOnRoot (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:20346:9)
        at performWork (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:20254:7)
        at performSyncWork (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:20228:3)
        at requestWork (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:20097:5)
        at scheduleWork (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:19911:5)
        at Object.enqueueSetState (~/workspace/app/node_modules/react-dom/cjs/react-dom.development.js:11169:5)
        at Provider.Object.<anonymous>.Component.setState (~/workspace/app/node_modules/react/cjs/react.development.js:335:16)
        at ~/workspace/app/node_modules/react-redux/lib/components/Provider.js:65:14
        at dispatch (~/workspace/app/node_modules/redux/lib/redux.js:220:7)
        at ~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1412:22
        at ~/workspace/app/node_modules/connected-react-router/lib/middleware.js:29:18
        at dispatch (~/workspace/app/node_modules/redux/lib/redux.js:617:28)
        at ~/workspace/app/node_modules/@redux-saga/core/dist/chunk-774a4f38.js:126:12
        at ~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:476:54
        at exec (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:35:5)
        at flush (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:91:5)
        at asap (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:50:5)
        at runPutEffect (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:472:3)
        at runEffect (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1202:7)
        at digestEffect (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1269:5)
        at next (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1159:9)
        at proc (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1106:3)
        at runCallEffect (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:537:7)
        at runEffect (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1202:7)
        at digestEffect (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1269:5)
        at next (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1159:9)
        at currCb (~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1249:7)
        at ~/workspace/app/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:400:5
        at processTicksAndRejections (internal/process/task_queues.js:86:5)".

      at getActiveElement (node_modules/react-dom/cjs/react-dom.development.js:5072:16)
      at getActiveElementDeep (node_modules/react-dom/cjs/react-dom.development.js:5353:17)
      at getSelectionInformation (node_modules/react-dom/cjs/react-dom.development.js:5383:21)
      at prepareForCommit (node_modules/react-dom/cjs/react-dom.development.js:8720:26)
      at commitRoot (node_modules/react-dom/cjs/react-dom.development.js:18872:3)
      at node_modules/react-dom/cjs/react-dom.development.js:20418:5
      at Object.unstable_runWithPriority (node_modules/scheduler/cjs/scheduler.development.js:255:12)
      at completeRoot (node_modules/react-dom/cjs/react-dom.development.js:20417:13)
      at performWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:20346:9)
      at performWork (node_modules/react-dom/cjs/react-dom.development.js:20254:7)
      at performSyncWork (node_modules/react-dom/cjs/react-dom.development.js:20228:3)
      at requestWork (node_modules/react-dom/cjs/react-dom.development.js:20097:5)
      at scheduleWork (node_modules/react-dom/cjs/react-dom.development.js:19911:5)
      at Object.enqueueSetState (node_modules/react-dom/cjs/react-dom.development.js:11169:5)
      at Provider.Object.<anonymous>.Component.setState (node_modules/react/cjs/react.development.js:335:16)
      at node_modules/react-redux/lib/components/Provider.js:65:14
      at dispatch (node_modules/redux/lib/redux.js:220:7)
      at node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1412:22
      at node_modules/connected-react-router/lib/middleware.js:29:18
      at dispatch (node_modules/redux/lib/redux.js:617:28)
      at node_modules/@redux-saga/core/dist/chunk-774a4f38.js:126:12
      at node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:476:54
      at exec (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:35:5)
      at flush (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:91:5)
      at asap (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:50:5)
      at runPutEffect (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:472:3)
      at runEffect (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1202:7)
      at digestEffect (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1269:5)
      at next (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1159:9)
      at proc (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1106:3)
      at runCallEffect (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:537:7)
      at runEffect (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1202:7)
      at digestEffect (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1269:5)
      at next (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1159:9)
      at currCb (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1249:7)
      at node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:400:5
      at processTicksAndRejections (internal/process/task_queues.js:86:5)".
      at BufferedConsole.error (node_modules/@jest/console/build/BufferedConsole.js:163:10)
      at Object.logError [as onError] (node_modules/@redux-saga/core/dist/chunk-774a4f38.js:104:11)
      at end (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1003:13)
      at abort (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:817:5)
      at Object.task.cont (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:832:9)
      at end (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1012:10)
      at abort (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:817:5)
      at Object.task.cont (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:832:9)
      at end (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1012:10)
      at abort (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:817:5)


  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "The above error occurred in task errorHandler
        created by handleFetchRoles
        created by AppSaga
        created by rootSaga
    Tasks cancelled due to error:
    handleSearchInput
    handleSearchClear
    handleNewUser
    handleSort
    handleInitPage
    handleDeleteUser
    usersSaga".

      at BufferedConsole.error (node_modules/@jest/console/build/BufferedConsole.js:163:10)
      at Object.logError [as onError] (node_modules/@redux-saga/core/dist/chunk-774a4f38.js:105:11)
      at end (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1003:13)
      at abort (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:817:5)
      at Object.task.cont (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:832:9)
      at end (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1012:10)
      at abort (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:817:5)
      at Object.task.cont (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:832:9)
      at end (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1012:10)
      at abort (node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:817:5)

@jasonsaayman
Copy link
Member

@jmelvin-r7 thanks for the pull request, I am going to look at reviewing it tonight. @favna I will have a look at trying to replicate this.

@favna
Copy link

favna commented Jun 4, 2019

@jasonsaayman thanks you. I cant share a lot of code as it's private company code but I can say it's TypeScript based and using React, Redux-Saga (and Redux then ofc).

@jasonsaayman
Copy link
Member

@favna, @jmelvin-r7 I pulled this, then had a look and I had the same issue as you guys described. I also agree with @masterots that pull request #2201 does in face mend this and does not break anything else. I cannot merge so we will have to wait for @emilyemorehouse to get to this, I will change the labels appropriately.

@jmelvin-r7
Copy link

Awesome, thanks! I'll let my team know.

@jmelvin-r7
Copy link

Also, @masterots is the same person as @jmelvin-r7 😁, separate work/personal accounts.

@favna
Copy link

favna commented Jun 6, 2019

@jasonsaayman Thanks! Looking forward to the future release to fix the issue.

sebelga added a commit to sebelga/kibana that referenced this issue Jun 13, 2019
Version 0.19.0 has breaking change and does not work with the Sinon mock server
Issue: axios/axios#2195
sebelga added a commit to sebelga/kibana that referenced this issue Jun 13, 2019
Version 0.19.0 has a breaking change and is not compatible anymore with the Sinon mock server.
Issue: axios/axios#2195
@jmelvin-r7
Copy link

So this is closed, but the PR is not not merged in the axios repo. Does this mean that we're just SOL?

@favna
Copy link

favna commented Aug 20, 2019

Tbh I switched out frontend projects over to GitHub's whatwg-fetch because of this issue. It's absolutely ridiculous how long it takes the devs to release anything here and in terms of trends axios is an absolute joke compared to whatwg- fetch:

image

source: https://www.npmtrends.com/axios-vs-whatwg-fetch (recorded at August 20th 2019, 09:12 PM UTC+0)

Also because whatwg-fetch uses browser fetch as its API you don't need additional typings when writing TypeScript, just need to specify the DOM lib, and it has a synonymous API in the backend with node-fetch

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
5 participants