btholt / btholt/complete-intro-to-react

actionCreators.spec.js should call done.fail() on failure

Open
#75 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.1k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

On branch `v3-26`, test [actionCreators.spec.js](https://github.com/btholt/complete-intro-to-react/blob/v3-26/js/__tests__/actionCreators.spec.js). you have this test:

```javascript
test('getAPIDetails', (done: Function) => {
const dispatchMock = jest.fn();
moxios.withMock(() => {
getAPIDetails(strangerThings.imdbID)(dispatchMock);
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request
.respondWith({
status: 200,
response: strangerThings
})
.then(() => {
expect(request.url).toEqual(`http://localhost:3000/${strangerThings.imdbID}`);
expect(dispatchMock).toBeCalledWith(addAPIData(strangerThings));
done();
});
});
});
});
```
The problem is that if one of the `expect` fails, then the error the test reports is a timeout because you didn't call `done()`.

I found that the `done` function has a [subfunction called `fail`](https://jasmine.github.io/2.3/introduction.html#section-54) that you can call to report a failure. So, you need to do `done.fail(error)` when an error occurs.

Since you are in a `then` of a promise, adding a `.catch(done.fail)` should do it.

In other words, your test should be:

```javascript
test('getAPIDetails', (done: Function) => {
const dispatchMock = jest.fn();
moxios.withMock(() => {
getAPIDetails(strangerThings.imdbID)(dispatchMock);
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request
.respondWith({
status: 200,
response: strangerThings
})
.then(() => {
expect(request.url).toEqual(`http://localhost:3000/${strangerThings.imdbID}`);
expect(dispatchMock).toBeCalledWith(addAPIData(strangerThings));
done();
}).catch(done.fail); // Fail fast and with nice error in case of expectation failures.
});
});
});
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.