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

onGet and other methods is not defined #377

Open
Shub1nk opened this issue Sep 5, 2023 · 11 comments
Open

onGet and other methods is not defined #377

Shub1nk opened this issue Sep 5, 2023 · 11 comments

Comments

@Shub1nk
Copy link

Shub1nk commented Sep 5, 2023

I tried to update axios to 1.2.4 in my project three months ago. But it failed.

When I launched Jest, I got one of these errors: onGet/onPost/reset is not defined or Error: connect ECONNREFUSED 127.0.0.1:80

I found some discussions, where was written that metod getAdapter is not public in the new version of axios.

https://github.com/ctimmerm/axios-mock-adapter/issues/355)](https://github.com/ctimmerm/axios-mock-adapter/issues/355

https://github.com/axios/axios/issues/5474)](https://github.com/axios/axios/issues/5474

Method getAdapter was made public in axios 1.5.0. In the lastest version of axios-mock-adapter version of axios is 0.27.2.

Do I understand correctly that this library can't work with projects, which have axios version higher than 0.27.2?

If it's true, do you plan to fix this problem?

Or am I doing something incorrectrly?

My project dependecies:

"jest": "26.5.0",
"axios": "1.5.0",
"axios-mock-adapter": "1.21.5"

My test example:

import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const http = axios.create({
    baseURL: `${process.ENV.SERVICE_API}`,
    headers: {
        'Content-Type': 'application/json',
    },
});

describe('fetchAssetTypes', () => {
    beforeAll(() => {
        mock = new MockAdapter(http);
    });
    afterAll(() => {
        mock.reset();
    });

    it('success', async () => {
        mock.onGet(/\/asset-types.*/).reply(200, mockTags);

        const result = await fetchAssetTypes({ codes: 'AP,AO' });

        expect(result).toEqual(mockTags);
    });
});

I have this error:

  TypeError: mock.onGet is not a function

      16 |     beforeAll(() => {
      17 |         mock = new MockAdapter(http);
    > 18 |         mock.onGet(/\/instruments.*/).reply(200, mockData);
         |              ^
      19 |         mock.onGet(/\/asset-types.*/).reply(200, mockTags);
      20 |     });
      21 |

Running coverage on untested files...node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: 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(). The promise rejected with the reason "Error: connect ECONNREFUSED 127.0.0.1:80".] {
  code: 'ERR_UNHANDLED_REJECTION'
}
@KonradFak
Copy link

Facing same issue. Does anyone had that and fixed it or do I need to replace library for mocking axios?

@Shub1nk
Copy link
Author

Shub1nk commented Sep 13, 2023

I found out when I run mock in Storybook then all work. I made example with mocking inside this library and this example work too.

But, when I run Jest when I use axios-mock-adapter inside tests then new MockAdapter begin to return Promise and I get error mock.onGet is not a function.

I don't understand what is going on.

@marcbachmann
Copy link
Collaborator

Must be Jest specific. I have axios 1.5 working with axios-mock-adapter

@Shub1nk
Copy link
Author

Shub1nk commented Sep 14, 2023

@marcbachmann Can you share your Jest config?

I have next settings in Jest for axios:

transformIgnorePatterns: ['/node_modules/(?!axios)'],
    moduleNameMapper: {
        axios: '<rootDir>/node_modules/axios/dist/node/axios.cjs',
    },

People from other releases write that jest, axios and axios-mock-adapter work for them with these settings. For some reason it doesn't work for me

@marcbachmann
Copy link
Collaborator

sorry, not using jest. that's why it must be jest specific 😅

@Shub1nk
Copy link
Author

Shub1nk commented Sep 14, 2023

it works with axios 0.27.2. When I was updated axios to 1.5 it broke

@KonradFak
Copy link

KonradFak commented Sep 15, 2023

I've manage to fix it in my case by bumping versions of jest and ts-jest. My current versions are:

  • axios 1.5.0
  • axios-mock-adapter 1.21.5
  • jest 29.7.0
  • ts-jest 29.1.1

Thanks @marcbachmann for pointing out the right direction 😄

@Shub1nk
Copy link
Author

Shub1nk commented Sep 25, 2023

I created test repo with example https://github.com/Shub1nk/test-jest-and-axios-mock-adapter

I use jest@29, axios@1.5 and axios-mock-adapter@1.22.0 and it work. But if I enable these options in jest's config, I will get error that mock.onGet is not function (mock -> Promise) again.

// ATTENTION: Test with axios mock fails if I enable these options
transformIgnorePatterns: ['/node_modules/(?!axios)'],
moduleNameMapper: {
axios: '<rootDir>/node_modules/axios/dist/node/axios.cjs',
 },

@richenyadav001
Copy link

Multiple libraries are affected due to axios, I tried with msw, nock and this one none of them is working properly axios@1.5.0

@scosc
Copy link

scosc commented Oct 31, 2023

Same issue with axios@1.6.0

@mustaphaturhan
Copy link

Looks like changing moduleNameMapper from axios to ^axios$ makes it work:

  moduleNameMapper: {
    '^axios$': 'axios/dist/node/axios.cjs',
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants