我有一个带有链式 api 调用的函数。我在两个 API 调用之间添加了 rxjs
delay
。
如何在 Jest 中编写测试用例来处理两个 API 调用之间的
delay
。
API 顺序
switchMap
致电delay
的5000
之后。switchMap
进一步的条件代码来检查条件您需要计时器模拟。
https://testing-library.com/docs/using-fake-timers/
https://jestjs.io/docs/timer-mocks
示例:
describe('Test', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});
test('should add a message', () => {
messageService.add(message);
jest.runOnlyPendingTimers();
expect(spy).toHaveBeenCalled();
});
});