[Vuejs]-How to test vue component method with ajax request in Mocha?

0👍

Advised by my leader, I find one magic config option of sinon fake server as follows

before(function () { 
    server = sinon.fakeServer.create(); 
    //mock response for ajax request
    server.respondWith("/api/nodesWithTask",[200,{"Content-Type":"application/json"},JSON.stringify(mockData)]);
    server.respondImmediately = true;
    const Constructor = Vue.extend(demo);
    vm = new Constructor().$mount();
  });

After I set the respondImmediately option to true, the test can run correctly.

Leave a comment