Тест выполняется синхронно, и expect
запускается и завершается неудачей, прежде чем обратные вызовы Promise
имеют шанс на выполнение.
Убедитесь, что вы await
Promise
вернули DocumentService.downloadDocumentById
, чтобы датьу обратных вызовов есть шанс запустить:
it("should call Document Service and download a document", async () => { // use an async test function
let catchFn = jest.fn(),
thenFn = jest.fn();
const promise = DocumentService.downloadDocumentById(jwtToken, DocumentURL, id)
.then(thenFn)
.then(catchFn); // remember the Promise
expect(axios.get).toHaveBeenCalledWith(DocumentURL);
let responseObj = { data: mockedData };
axios.get.Mockresponse(responseObj);
await promise; // wait for the Promise
expect(thenFn).toHaveBeenCalledWith(mockedData); // SUCCESS
expect(catchFn).not.toHaveBeenCalled(); // SUCCESS
});