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

problem with custom instances #24

Closed
paulvanbladel opened this issue Apr 7, 2017 · 4 comments
Closed

problem with custom instances #24

paulvanbladel opened this issue Apr 7, 2017 · 4 comments

Comments

@paulvanbladel
Copy link

I have following file with a custom axios instance:

import axios from 'axios';
import {AxiosInstance} from 'axios';
import { Loading } from 'quasar';

let axiosInstance = axios.create({
  baseURL: 'http://localhost:8080/api/'
});

axiosInstance.interceptors.request.use(function (config) {
  Loading.show();
  return config;
});

axiosInstance.interceptors.response.use(function (response) {
  Loading.hide();
  return response;
}, function (error) {
  Loading.hide();
  return Promise.reject(error);
});


export default axiosInstance;

I consume this file in processing and in a test, but it doesn't work.
When I use the default instance, everything works as expected:

Inside a VueJs model is do:

import axiosInstance from '../../configs/axiosInstance';
...
axiosInstance.get(todos). then ....

And in the test I use moxios as follows:

import axiosInstance from '../../configs/axiosInstance';
moxios.install(axiosInstance);

moxios.stubRequest('todos', {
    status: 200,
    response:
       [
         {
          'userId': 1,
          'id': 1,
          'title': 'delectus aut autem',
          'completed': false
        },
        {
          'userId': 1,
          'id': 2,
          'title': 'quis ut nam facilis et officia qui',
          'completed': false
        }
      ]
    }
  )

  const vm = new CardTodoComponent();


 vm.$mount();
 expect(vm.todos.length).equal(2);
 done();

As I said, when I switch both in the test and the processing to the default axios instance, the fails succeeds and get the amount of records.

What am I doing wrong?

@paulvanbladel
Copy link
Author

paulvanbladel commented Apr 14, 2017

I can confirm that the update from nicangeli fixes the problem.
I saw a PR is already there.
Thanks a lot.

Tobbe added a commit to Tobbe/moxios that referenced this issue Jun 26, 2017
Include baseUrl when comparing URLs

Fixes axios#24
@anilanar
Copy link

Kamino cloned this issue to anilanar/moxios

@or4
Copy link

or4 commented Nov 8, 2019

short answer, you should use

let instance = axios.create({ baseUrl: 'example.com' })
moxios.install(instance)

thanks a lot for pr

@Quirksmode
Copy link

@or4 I tried your code snippet, but it doesn't seem to work for me. When I log the axios vs the moxios instance code (with the baseUrl added), they look exactly the same so I am not sure what the issue could be. Your code makes perfect sense that it would get this working, so i'm stumped as to why it isn't.

This is the axios code I pass to my mocked store
const instance = axios.create({ baseURL: 'http://www.example.com/wp-json' });

This is the moxios code
beforeEach(() => { const instance = axios.create({ baseURL: 'http://www.example.com/wp-json' }); moxios.install(instance); });

mzabriskie added a commit that referenced this issue Jun 10, 2020
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

4 participants