0

I find myself writing this at the start of pretty much all of my unit tests in mocha:

it('should do something', (done) => {
  Vue.config.errorHandler = done;

  // do something aynchronous
});

By default, Vue catches all errors itself and logs them to the console, so mocha can't see them. This code makes sure that thrown errors fail the tests.

Is there a way with mocha to do this without having to start every single async test with this line of code? If I have to write / use a plugin, that's fine.

2
  • I don't use Mocha, but every test framework I've ever used has some kind of setup and teardown method to handle these kinds of things. Commented Nov 28, 2017 at 11:04
  • @craig_h yep, there's a beforeEach hook. It can only be used to fail the test before it runs, not during the testing stage. Commented Nov 28, 2017 at 11:05

1 Answer 1

-1

Try:

Vue.config.errorHandler = function (err, vm, info) {
  throw err
}

in your test entry.

Sign up to request clarification or add additional context in comments.

4 Comments

This won't work if the error handler is called in a promise.
It works in my test. Try change your async test from done call to async functions. e.g. it('should...', async () => { //... }) @callumacrae
This fails: Vue.nextTick(() => { throw new Error('test'); }); - Vue.nextTick() uses promises internally.
Well that was expected. A promise is designed to catch errs internally. If you insist to throw them you can do this var originalCatch = Promise.prototype.catch; Promise.prototype.catch = function(){throw new Error();return originalCatch.apply(this, arguments);}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.