axios-mock-adapter

How do you verify that a request was made with axios-mock-adapter?

一笑奈何 提交于 2020-05-26 10:43:39
问题 I am using https://github.com/ctimmerm/axios-mock-adapter I would like to know how can I verify that an endpoint was actually called by the system under test. In this example: var axios = require('axios'); var MockAdapter = require('axios-mock-adapter'); // This sets the mock adapter on the default instance var mock = new MockAdapter(axios); // Mock any GET request to /users // arguments for reply are (status, data, headers) mock.onGet('/users').reply(200, { users: [ { id: 1, name: 'John

Why TypeError: axios.create is not a function? When testing axios GET

Deadly 提交于 2020-05-14 18:35:51
问题 I'm trying to test my axios API functions in React. Found this question here: how do i test axios in jest which pointed to using axios-mock-adapter import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import chatbot from './chatbot'; describe('Chatbot', () => { it('returns data when sendMessage is called', done => { var mock = new MockAdapter(axios); const data = { response: true }; mock.onGet('https://us-central1-hutoma-backend.cloudfunctions.net/chat').reply(200, data);

Why TypeError: axios.create is not a function? When testing axios GET

泄露秘密 提交于 2020-05-14 18:35:12
问题 I'm trying to test my axios API functions in React. Found this question here: how do i test axios in jest which pointed to using axios-mock-adapter import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import chatbot from './chatbot'; describe('Chatbot', () => { it('returns data when sendMessage is called', done => { var mock = new MockAdapter(axios); const data = { response: true }; mock.onGet('https://us-central1-hutoma-backend.cloudfunctions.net/chat').reply(200, data);

Axios catch error Request failed with status code 404

房东的猫 提交于 2019-12-21 04:59:06
问题 I'm testing a login component that uses Axios. I tried mocking Axios with axios-mock-adapter , but when I run the tests, it still errors out with: Error: Request failed with status code 404 How do I properly mock Axios in my tests? login.spec.js: import Vue from 'vue' import { shallowMount, createLocalVue } from '@vue/test-utils'; import Login from '../../src/components/global/login/Login.vue'; import Raven from "raven-js"; import jQuery from 'jquery' import Vuex from 'vuex' import router

Is it possible to apply passThrough() within a mock reply using axios-mock-adapter?

只谈情不闲聊 提交于 2019-12-13 16:56:41
问题 Environment: NodeJS 8.1.2 axios 0.16.2 axios-mock-adapter 1.9.0 Testing a JSON-RPC endpoint, am I able to do something like this: const mockHttpClient = new MockAdapter(axios, { delayResponse: 50 }) mockHttpClient.onPost().reply((config) => { // Capture all POST methods const dataObj = JSON.parse(config.data) // Example data: '{"jsonrpc":"2.0","method":"getProduct","params":[123],"id":0}' if (dataObj.method === 'getProduct') { // Recognised method, provide mock response override return [200,

React-Redux app testing action creator containing axios GET call

我是研究僧i 提交于 2019-12-12 04:04:13
问题 I am writing a react-redux app and in one of my action creators I am making an api request via axios Roughly, it looks something like this: import axios from 'axios' export function getStoredData(userInput) { . . . var url = generateURL(userInput); var response = axios.get(url); return { type: GET_STORED_DATA payload: repsonse; }; } I wanted to mock axios using axios-mock-adapter but I don't quite understand how to do that, especially since I wouldn't want to modify my action creator for the

Mock axios with axios-mock-adapter get undefined resp

荒凉一梦 提交于 2019-12-07 04:29:13
问题 I created an axios instance ... // api/index.js const api = axios.create({ baseURL: '/api/', timeout: 2500, headers: { Accept: 'application/json' }, }); export default api; And severals modules use it .. // api/versions.js import api from './api'; export function getVersions() { return api.get('/versions'); } I try to test like .. // Test import { getVersions } from './api/versions'; const versions= [{ id: 1, desc: 'v1' }, { id: 2, desc: 'v2' }]; mockAdapter.onGet('/versions').reply(200,

Mock axios with axios-mock-adapter get undefined resp

孤人 提交于 2019-12-05 10:47:59
I created an axios instance ... // api/index.js const api = axios.create({ baseURL: '/api/', timeout: 2500, headers: { Accept: 'application/json' }, }); export default api; And severals modules use it .. // api/versions.js import api from './api'; export function getVersions() { return api.get('/versions'); } I try to test like .. // Test import { getVersions } from './api/versions'; const versions= [{ id: 1, desc: 'v1' }, { id: 2, desc: 'v2' }]; mockAdapter.onGet('/versions').reply(200, versions); getVersions.then((resp) => { // resp is UNDEFINED? expect(resp.data).toEqual(versions); done();

Axios catch error Request failed with status code 404

笑着哭i 提交于 2019-12-03 20:23:24
I'm testing a login component that uses Axios. I tried mocking Axios with axios-mock-adapter , but when I run the tests, it still errors out with: Error: Request failed with status code 404 How do I properly mock Axios in my tests? login.spec.js: import Vue from 'vue' import { shallowMount, createLocalVue } from '@vue/test-utils'; import Login from '../../src/components/global/login/Login.vue'; import Raven from "raven-js"; import jQuery from 'jquery' import Vuex from 'vuex' import router from '../../src/router' var axios = require('axios'); var MockAdapter = require('axios-mock-adapter');

how to match a returned Promise in using Jest with redux action

独自空忆成欢 提交于 2019-12-02 18:58:11
问题 I am using reactjs with redux and the action is using axios and returning a Promise. my test is not matching the type. I have tried different ways of doing it but end up with the same problem. also tried https://www.leighhalliday.com/mocking-axios-in-jest-testing-async-functions import configureMockStore from 'redux-mock-store' //import fetchMock from 'fetch-mock' import expect from 'expect' // You can use any testing library import axios from 'axios'; import MockAdapter from 'axios-mock