diff --git a/src/utils/promise/wait/index.test.ts b/src/utils/promise/wait/index.test.ts new file mode 100644 index 0000000..6a96665 --- /dev/null +++ b/src/utils/promise/wait/index.test.ts @@ -0,0 +1,27 @@ +import { wait } from '.'; + +it('returns a promise that resolves after the specified amount of time', async () => { + jest.useFakeTimers(); + + let value: number = 1; + + wait(1000).then(() => { + value++; + }); + + expect(value).toBe(1); + + jest.advanceTimersByTime(500); + + await Promise.resolve(); + + expect(value).toBe(1); + + jest.advanceTimersByTime(500); + + await Promise.resolve(); + + expect(value).toBe(2); + + jest.useRealTimers(); +}); diff --git a/src/utils/promise/wait/index.ts b/src/utils/promise/wait/index.ts index 4908141..41786b4 100644 --- a/src/utils/promise/wait/index.ts +++ b/src/utils/promise/wait/index.ts @@ -2,4 +2,4 @@ * Returns a promise that resolves after the specified amount of time in milliseconds. * @param ms Amount in milliseconds to wait. */ -export const wait = (ms: number) => new Promise((res) => setTimeout(res, ms)) +export const wait = (ms: number) => new Promise(res => setTimeout(res, ms));