The state does not change when unit testing VueJS and VueResource

I am trying to test a service I created that calls an API call with a vue resource. The service should make a call and update the component with the new data, however in my tests it does not register the updated value. I use the same setup as vue-cli webpack and founded my authorization service this repo (unfortunately, there are no tests)

my service:

export default {
   login(context, creds){
      context.$http.post(LOGIN_URL, creds)
        .then((response) => {
           //do something else here
        }, (response) => {
           context.error = response.data.error
        }
   }
}

my test:

import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import Auth from 'src/auth'

describe('Auth', () => {
   it('should throw an error on unsuccessful login', (done) => {

     //intercept the api call and force an invalid response
     Vue.http.interceptors.unshift((request, next) => {
        next(request.respondWidth({error: 'some error'}, {status: 401}));
     });


     const vm = new Vue({
        data: {
           error: ''
        }
     }).$mount()

     Auth.login(vm, {email: 'test@test.com', pass: 'test'} )

     //this always fails
     expect(vm.error).to.equal('some error')

     //ive also tried:
     vm.$nextTick(() => {
        expect(vm.error).to.equal('some error')
        //done()
     });

     //undo our interceptor
     Vue.http.interceptors.shift()

   }
}

When I run the test, it fails because it expects an "equal" to some error.

My suspicions are that the vue resource is using promises.

+4
1

Vue.js . vm. $NextTick :

setTimeout(function(){
   expect(vm.error).to.equal('something')
   done()
}, 0)
+4

Source: https://habr.com/ru/post/1651241/


All Articles