vue.js Unit Test - Error in mounted hook
You may want to use Vue.config.errorHandler
API which is introduced since v2.2.0
.
You can declare a variable as an error-thrown flag (i.e.
errorThrown
).let errorThrown = false;
Define the errorHandler function so that
errorThrown
will be switched totrue
when it's triggered.Vue.config.errorHandler = function (err, vm, info) { console.log(info); if(info.includes('mounted')) { errorThrown = true; }};
Check
errorThrown
by usingexpect
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.