У меня есть контейнер, где у меня есть функция, которая делает запрос API, используя fetch
:
updateBooks = books => this.setState({ books });
getBooks = keyword => fetch(`${googleBooksSearchAPI}${keyword}`)
.then(response => response.json())
.then(myJson => this.updateBooks(myJson.items))
.catch(error => console.log('error', error));
Я пытаюсь написать для него тесты, используя fetch-mock
:
it('getBooks works', () => {
const wrapper = shallow(<BookContainer />);
const mockData = { items: bookListObj };
fetchMock.get(googleBooksSearchAPI, mockData);
wrapper.instance().getBooks('dun');
expect(wrapper.state().books).toEqual(bookListObj);
fetchMock.restore();
});
Все тесты показывают как пройденные, но все же я вижу эту ошибку:
console.log app/components/BookContainer.js:20
error FetchError {
name: 'FetchError',
message: 'invalid json response body at undefined reason: Unexpected end of JSON input',
type: 'invalid-json' }
если я удаляю catch
из выборки, это показывает следующее сообщение:
(node:5094) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at undefined reason: Unexpected end of JSON input
(node:5094) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:5094) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Как это убрать?