Skip to content

Vue.nextTick() and mocha don't play nice #7140

Closed
@callumacrae

Description

@callumacrae

Version

2.5.8

Reproduction link

https://jsbin.com/lujehap/edit?html,js,output

Steps to reproduce

Run tests in repro link :)

What is expected?

Both tests should fail with similar error messages

What is actually happening?

The first test, using setTimeout, fails as expected, but with the second test, using Vue.nextTick(), just times out, with the error disappearing.


This is happening because Vue.nextTick() apparently uses a promise internally and mocha doesn't support catching unhandled promise errors.

There is a workaround, but it isn't great:

window.addEventListener('unhandledrejection', (e) => {
  throw e;
});

That at least lets you know that there was an error, but doesn't log the correct error.

You can also wrap the contents of the test in setTimeout:

it('should catch errors from Vue.nextTick()', (done) => {
  Vue.nextTick(() => {
    setTimeout(() => {
      throw new Error('test');
      done();
    });
  });
});

That will ensure that mocha knows about them.

While this is mostly a problem with mocha, I think it is unexpected that you have to handle errors inside Vue.nextTick() as if in a promise - it doesn't look like a promise from the function!

I'm not really sure what I would suggest doing about this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions