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.
- [Vuejs]-Accessing Vue.$router inside an external "service" class
- [Vuejs]-Refreshing page on child route doesn't show the data for just that route
-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
- [Vuejs]-Vue.js: Known issues with scoped styles and the v-html directive?
- [Vuejs]-Imagekit SDK and NuxtJS: How to import SDK correctly?