vue.js Unit Test - Error in mounted hook vue.js Unit Test - Error in mounted hook vue.js vue.js

vue.js Unit Test - Error in mounted hook


You may want to use Vue.config.errorHandler API which is introduced since v2.2.0.

  1. You can declare a variable as an error-thrown flag (i.e. errorThrown).

    let errorThrown = false;
  2. Define the errorHandler function so that errorThrown will be switched to true when it's triggered.

    Vue.config.errorHandler = function (err, vm, info) {  console.log(info);  if(info.includes('mounted')) {    errorThrown = true;  }};
  3. Check errorThrown by using expect API in jasmine.

    expect(errorThrown).toBe(false);  // Test will fail here

Check the working demo here.
In this way, errors occurred in mounted method will be catched and force unit test to fail.