useFakeTimers и асинхронный обратный вызов

Использовать grep -P - который позволяет поддерживать регулярные выражения в стиле Perl.

grep -P "abc.*def" myfile
2
задан Sergio M. 18 January 2019 в 14:39
поделиться

1 ответ

Как было предложено Марком Мейером в комментариях, сделать startTimerWithAsyncCallback возвращение Обещания более удобным для тестирования

function startTimerWithAsyncCallback(
    firstAsyncFunction,
    secondAsyncFunction,
    thirdAsyncFunction,
    millis,
) {
    return new Promise((resolve) => {      // <==
        setTimeout(async () => {
            await firstAsyncFunction();
            await secondAsyncFunction();
            await thirdAsyncFunction();
            resolve();                     // <==
        }, millis);
    });
}


describe('Using async callbacks with timers', () => {
    test('fake timers', async () => {
        jest.useFakeTimers();

        const firstAsyncFunction = jest.fn();
        const secondAsyncFunction = jest.fn();
        const thirdAsyncFunction = jest.fn();

        const promise = startTimerWithAsyncCallback( // <==
            firstAsyncFunction,
            secondAsyncFunction,
            thirdAsyncFunction,
            1000,
        );
        jest.advanceTimersByTime(2000);

        await promise;  <==

        expect(firstAsyncFunction).toHaveBeenCalled();
        expect(secondAsyncFunction).toHaveBeenCalled();
        expect(thirdAsyncFunction).toHaveBeenCalled();
    });
});
0
ответ дан Sergio M. 18 January 2019 в 14:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: