question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jasmine-Ajax and Vue Resource

See original GitHub issue

Hello, i’m trying to unit test my vue-resource ajax requests but jasmine-ajax does not seem to be able to picking up and intercepting the ajax request. I had jasmine-ajax working when i use jQuery ajax, but this doenst seem to be playing ball.

My Code:

it('should use the delete method', function(){
    var vm = new Vue({
    template: '<div><test :status="status" :entity="entity" :detailspage="" :user="user" v-ref:action-comp></test></div>',


    components: {
    'test': Action
    }, 
    data: {
    status: 'processing',
    entity: {entity_id: '123'},
    user: 'standard'
    }

    }).$mount();

    vm.$refs.actionComp.cancelItem(vm.entity, 'true');

    var request = jasmine.Ajax.requests.mostRecent();

    expect(request.method).toEqual('DELETE');

    });
/* my vue-resource */
vm.$http.delete('/api-url/' + entity.entity_id)
                .then(function (res){
                    entity.status = 'cancelled';
                    vm.getUpdatedList();
                }, function (res) {
                    console.log('testing error');
                });`

it just does not intercept so the error is always fired.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

5reactions
mcmillioncommented, Mar 29, 2017

Why was this closed? This appears to be an ongoing issue.

0reactions
Doogiemuccommented, Jul 27, 2016

I am searching in the same area. My tests are running with karma+webpack+mocha+sinonjs (This is how the large vue webpack template sets up everything.) And I have the same problem with vue-resource.

In sinon.js one can setup a sinon.fakeServer.create() that should intercept all AJAX calls, but for some reasons, it does not intercept the calls by vue-resource. Somehow vue-resource fiddles areound with XMLHttpRequest on its own.

The only way I got it working was with another method, by using vue-resource’s own interceptorlike this in my test spec:

    Vue.use(require('vue-resource'));
    Vue.http.interceptors.push((request, next) => {
      if (request.url == '/tabletestdata') {
        console.log('Intercepting request to '+request.url+ '. Sending reply with canned testdata.')
        // stop and return response
        next(request.respondWith(testData, { status: 200, statusText: 'Ok' }))
      } else {
        next()
      }
    });

I’d assume that jasmine.Ajax.request hangs on the same problem than sinon.fakeServer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue 2 Unit Testing Primer - Medium
This article will take you through the process of building and testing a small Vue 2 application with Jasmine.
Read more >
Sending AJAX Requests in Vue.js - Stack Abuse
Vue.js has an official package which is called vue-resource which works as an HTTP client, but the official documentation suggests using Axios.
Read more >
vue-resource | Yarn - Package Manager
The plugin for Vue.js provides services for making web requests and handle responses using a XMLHttpRequest or JSONP. Features. Supports the Promise API...
Read more >
Replace vue-resource and $.ajax with axios to be the default HTTP ...
Replace vue-resource with axios; Update docs; Replace $.ajax with axios - not as ... we depend on karma-jasmine which depends on jQuery, but...
Read more >
AJAX Requests in Vue.js: Axios vs vue-resource vs fetch
AJAX Requests in Vue.js: Axios vs vue-resource vs fetch. Consuming data from remote API servers via API calls, HTTP requests or Ajax ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found