Skip to content

Commit

Permalink
Add tests for wait util
Browse files Browse the repository at this point in the history
  • Loading branch information
kyarik committed Nov 28, 2020
1 parent 6562e40 commit c2c7b13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/utils/promise/wait/index.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
2 changes: 1 addition & 1 deletion src/utils/promise/wait/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

0 comments on commit c2c7b13

Please sign in to comment.