Skip to content

Commit cb2682f

Browse files
✅ synchronously run Promise.resolve when using the mock clock
1 parent bb5897f commit cb2682f

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

packages/core/test/emulate/mockClock.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export function mockClock() {
1515

1616
registerCleanupTask(() => jasmine.clock().uninstall())
1717

18+
const pendingMicroTasks: Array<() => void> = []
19+
20+
const originalPromiseResolve = Promise.resolve.bind(Promise)
21+
spyOn(Promise, 'resolve').and.callFake(
22+
() =>
23+
({
24+
then: (callback: () => void) => {
25+
pendingMicroTasks.push(callback)
26+
return originalPromiseResolve()
27+
},
28+
}) as Promise<void>
29+
)
30+
1831
return {
1932
/**
2033
* Returns a RelativeTime representing the time it was X milliseconds after the `mockClock()`
@@ -26,7 +39,10 @@ export function mockClock() {
2639
* invokation (the start of the test).
2740
*/
2841
timeStamp: (duration: number) => (timeStampStart + duration) as TimeStamp,
29-
tick: (ms: number) => jasmine.clock().tick(ms),
42+
tick: (ms: number) => {
43+
pendingMicroTasks.splice(0).forEach((task) => task())
44+
jasmine.clock().tick(ms)
45+
},
3046
setDate: (date: Date) => jasmine.clock().mockDate(date),
3147
}
3248
}

0 commit comments

Comments
 (0)