🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 354
Cannot find module 'axios-mock-adapter' or its corresponding type declarations.ts · Issue #354 · ctimmerm/axios-mock-adapter
December 15, 2022 - I am getting an error about the type declaration :(( Cannot find module 'axios-mock-adapter' or its corresponding type declarations.ts Here are my versions { "name": "control-center-ui", "version": "0.1.0", "private": true, "dependencies...
Author   seda1094
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 400
TypeScript error when axios instance is created from an import that uses `axios >=1.6.0`'s ESM types · Issue #400 · ctimmerm/axios-mock-adapter
October 10, 2024 - With axios@1.7.7, axios-mock-adapter@2.1.0, typescript@5.6.3, using MockAdapter causes TypeScript to error about type differences between axios when imported via our ESM code and the types imported by axios-mock-adapter. The newest axios version that this does not occur is axios@1.5.1, the type change seems to be introduced in axios@1.6.0.
Author   evelynhathaway
🌐
npm
npmjs.com › package › axios-mock-adapter
axios-mock-adapter - npm
October 9, 2024 - const axios = require("axios"); const AxiosMockAdapter = require("axios-mock-adapter"); // This sets the mock adapter on the default instance const mock = new AxiosMockAdapter(axios); // Mock any GET request to /users // arguments for reply are (status, data, headers) mock.onGet("/users").reply(200, { users: [{ id: 1, name: "John Smith" }], }); axios.get("/users").then(function (response) { console.log(response.data); });
      » npm install axios-mock-adapter
    
Published   Oct 09, 2024
Version   2.1.0
Author   Colin Timmermans
🌐
npm
npmjs.com › package › @types › axios-mock-adapter
types/axios-mock-adapter
Author message:This is a stub types definition. axios-mock-adapter provides its own type definitions, so you do not need this installed.
      » npm install @types/axios-mock-adapter
    
Published   Oct 23, 2024
Version   1.10.4
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 371
Types are broken with CJS/Typescript · Issue #371 · ctimmerm/axios-mock-adapter
January 24, 2023 - error TS1259: Module '".../node_modules/axios-mock-adapter/types/index"' can only be default-imported using the 'esModuleInterop' flag index.d.ts(81, 1): This module is declared with 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
Author   orgads
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 166
Unable to mock axios instances · Issue #166 · ctimmerm/axios-mock-adapter
August 16, 2018 - So my guessing is that the mock can only be applied to the default instance of axios, and not on subsequently created instances. Is my guess correct? The only workaround I found is to add a condition to the default export in client.js:
Author   ingro
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues
Issues · ctimmerm/axios-mock-adapter
Axios adapter that allows to easily mock requests. Contribute to ctimmerm/axios-mock-adapter development by creating an account on GitHub.
Author   ctimmerm
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 367
Typescript compile error with 1.21.3 · Issue #367 · ctimmerm/axios-mock-adapter
March 26, 2023 - [~/git/koopa-api] yarn tsc:check yarn run v1.22.19 $ tsc --noEmit node_modules/axios-mock-adapter/types/index.d.ts:55:6 - error TS2702: 'AxiosAdapter' only refers to a type, but is being used as a namespace here. 55 ) => AxiosAdapter.RequestHandler; ~~~~~~~~~~~~ Found 1 error in node_modul...
Published   Mar 27, 2023
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter
GitHub - ctimmerm/axios-mock-adapter: Axios adapter that allows to easily mock requests
const axios = require("axios"); const AxiosMockAdapter = require("axios-mock-adapter"); // This sets the mock adapter on the default instance const mock = new AxiosMockAdapter(axios); // Mock any GET request to /users // arguments for reply are (status, data, headers) mock.onGet("/users").reply(200, { users: [{ id: 1, name: "John Smith" }], }); axios.get("/users").then(function (response) { console.log(response.data); });
Starred by 3.5K users
Forked by 255 users
Languages   JavaScript 96.2% | TypeScript 3.8% | JavaScript 96.2% | TypeScript 3.8%
Find elsewhere
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 331
include supported axios minor version near the top of documentation · Issue #331 · ctimmerm/axios-mock-adapter
March 4, 2022 - Type 'AxiosRequestConfig' is not assignable to type 'AxiosDefaults<any>'. Property 'headers' is optional in type 'AxiosRequestConfig' but required in type 'AxiosDefaults<any>'. ... import axios from 'axios' import MockAdapter from 'axios-mock-adapter' describe('sociable Controller test', () ...
Author   xenoterracide
Top answer
1 of 1
3

There are a couple of problems in your code:

First, in the action creator you are using an axios instance to make the ajax call, but in the test you are not providing that instance to the axios-mock-adapter. You should provide your axios instance in your test when you create the instance of MockAdapter.

Second, the params property you are providing to the axios mock in the onGet method does not match the parameters that are sent in the get operation in your action creator. You should match the parameters in the call with their values. Thus, you should provide query and page params.

Last, you are returning the expectedActions in the mock request, but that does not seem right. Looking at your code, it seems that you want to return an empty array.

Having all that into account, your code would look like:

import MockAdapter from 'axios-mock-adapter';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';

import axiosInstence from '../../apis/axiosInstence';
import { searchPhotos } from '../searchAction';

const mock = new MockAdapter(axiosInstence);

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const term = 'cars';
const store = mockStore({});

describe('actions', () => {
    beforeEach(() => {
        mock.reset();
        store.clearActions();
    });

    it('Should create an action to signIn with a fake user', async () => {
        const expectedActions = [{
            type: 'SEACH_REQUEST'
        }, {
            type: 'SEARCH_PHOTOS',
            payload: []
        }];

        mock.onGet('/search/photos', {
            params: {
                query: 'cars',
                page: 1
            }
        }).reply(200, []);

        const data = await store.dispatch(searchPhotos(term));
        const receivedActions = store.getActions();
        expect(receivedActions).toEqual(expectedActions);
    });
});
Top answer
1 of 2
5

First of all: you have to use the same axios instance with both places: to setup the mock response and to make the actual call. When I want to mock some API I usually create and export the axios instance in a separate file, and just import it where I need it. like so:

// dataAPI.js
import axios from 'axios';

export const dataAPI = axios.create(); // general settings like baseURL can be set here
// FeatureTable.js
import React, { useEffect } from 'react';
import { dataAPI } from './dataAPI';

export const FeatureTable = () => {
  useEffect(() => {
    dataAPI.get('http://localhost:8080/users').then(something);
  }, []);

  return (<div>something</div>);
};
// mock.js
import { dataAPI } from './dataAPI';
import MockAdapter from 'axios-mock-adapter';
import { users } from './mockData.js'

const mock = new MockAdapter(dataAPI);
mock.onGet('http://localhost:8080/users').reply(200, users);

I want to point out axios-response-mock as an alternative to axios-mock-adapter. Disclaimer: I'm the author of that library. I was a bit frustrated by mock-adapter because I didn't find the API intuitive and it has limitations (e.g. can't match URL params in conjunction with POST requests) and that it would not let unmatched requests pass-through by default.

2 of 2
-1

In your table file, you have to pass the relative path, not the absolute one.

const users = [{ id: 1, name: "John Smith" }];


const featureTable = (mock: any) => {
  mock.onGet("/users").reply(200, users);
}

export default featureTable;
🌐
Stack Overflow
stackoverflow.com › questions › tagged › axios-mock-adapter
Newest 'axios-mock-adapter' Questions - Stack Overflow
I have axios and axios-mock-adapter installed in my nextjs app, but when the Axios request running in localhost:3000 tries to call an AWS Lambda request (that I know works), I'm getting the error: ... ... I'm learning unit testing in vue with Typescript and I want to test this function: const getCLients =async (): Promise<Client[]> => { const {data} = await clientsApi.get('/clients') ...
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 28
How to use with ES6? · Issue #28 · ctimmerm/axios-mock-adapter
* EUI-9035: Case Flags v2 fix - Ensure Case Flag Summary (CYA) page displays flag update comments Upgrade `@hmcts/ccd-case-ui-toolkit` dependency to version `6.19.15-case-flags-v2-manage-case-flags-flag-update-comments-fix-2`, for testing bug fix for EUI-9035. * yarn audit update * temporary ignore test * library updates * version update * Replace outdated ES5 "require" syntax for MockAdapter dependency with ES6 import Switch to ES6 import statement (see ctimmerm/axios-mock-adapter#28).
🌐
CodeSandbox
codesandbox.io › examples › package › axios-mock-adapter
axios-mock-adapter examples - CodeSandbox
Use this online axios-mock-adapter playground to view and fork axios-mock-adapter example apps and templates on CodeSandbox.
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 161
Issue with Jest and TypeScript · Issue #161 · ctimmerm/axios-mock-adapter
August 28, 2018 - Whenever I try to use this lib with TypeScript like so: import MockAdapter from 'axios-mock-adapter/types'; I get Cannot find module 'axios-mock-adapter/types' from 'FooBar.test...
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › tree › master › types
axios-mock-adapter/types at master · ctimmerm/axios-mock-adapter
Axios adapter that allows to easily mock requests. Contribute to ctimmerm/axios-mock-adapter development by creating an account on GitHub.
Author   ctimmerm