[Vuejs]-Jest mock not used – normal method is run instead

1πŸ‘

In your test with a setTimeout, you need to use the done callback.

Currently, Jest is running the test and reaching the end without an error. To Jest, this means the test has passed. One way to get Jest to run the test asynchronously is to use the done callback.

If a test has the done callback, Jest won’t pass the test until the done callback has been called.

Your code should look like this:

it('should call a fail commit if request fails', (done) => {
  api.fetchItems = jest.fn(() => Promise.reject());
  setTimeout(() => {
    expect(commit).toHaveBeenCalledWith(types.FETCHED_ADS_FAIL);
    done()
  });
});

You can read about the [done callback in the Jest docs(http://facebook.github.io/jest/docs/en/asynchronous.html#callbacks).

There are a few other ways that Jest deals with asynchronous code.

πŸ‘€Edward

-1πŸ‘

In case anyone comes across this and needs to build a solution to fail when this happens, here is I’ve solved this: Handle UnhandledPromiseRejectionWarning Errors

πŸ‘€Christopher

Leave a comment