|
1 |
| -import { sleep } from '../async.utils'; |
| 1 | +import { createLogger } from '../../logger/logger.utils'; |
| 2 | +import { runInBackground, sleep } from '../async.utils'; |
| 3 | + |
| 4 | +jest.mock('../../logger/logger.utils', () => { |
| 5 | + const actualModule = jest.requireActual('../../logger/logger.utils'); |
| 6 | + return { |
| 7 | + ...actualModule, |
| 8 | + createLogger: jest.fn().mockReturnValue({ |
| 9 | + error: jest.fn(), |
| 10 | + }), |
| 11 | + }; |
| 12 | +}); |
2 | 13 |
|
3 | 14 | describe('async-utils', () => {
|
| 15 | + afterEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
4 | 19 | describe('#sleep', () => {
|
5 |
| - it('should resolve after 1000ms', async () => { |
6 |
| - jest.useFakeTimers(); |
| 20 | + test('it sleeps for the given milliseconds', async () => { |
| 21 | + const timeoutSpy = jest.spyOn(global, 'setTimeout'); |
7 | 22 |
|
8 |
| - let didSleep = false; |
| 23 | + const sleepMillis = 100; |
| 24 | + const varianceMillis = sleepMillis * 0.5; |
9 | 25 |
|
10 |
| - const fn = jest.fn().mockImplementation(async () => { |
11 |
| - await sleep(1000); |
12 |
| - didSleep = true; |
13 |
| - }); |
| 26 | + const start = Date.now(); |
| 27 | + |
| 28 | + await sleep(100); |
| 29 | + |
| 30 | + const end = Date.now(); |
| 31 | + |
| 32 | + expect(timeoutSpy).toBeCalledTimes(1); |
| 33 | + expect(end - start).toBeGreaterThanOrEqual(sleepMillis - varianceMillis); |
| 34 | + expect(end - start).toBeLessThanOrEqual(sleepMillis + varianceMillis); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('#runInBackground', () => { |
| 39 | + test('when given a callback then it runs in background', (done) => { |
| 40 | + runInBackground(done); |
| 41 | + }); |
14 | 42 |
|
15 |
| - expect(didSleep).toBe(false); // sanity check |
| 43 | + test('when callback throws then it propagates', (done) => { |
| 44 | + try { |
| 45 | + runInBackground(() => { |
| 46 | + throw new Error('test'); |
| 47 | + }); |
| 48 | + } catch (error) { |
| 49 | + done(); |
| 50 | + } |
| 51 | + }); |
16 | 52 |
|
17 |
| - fn(); // kick off promise in background |
| 53 | + test('when given a promise then it runs in background', (done) => { |
| 54 | + runInBackground(async () => { |
| 55 | + done(); |
| 56 | + }); |
| 57 | + }); |
18 | 58 |
|
19 |
| - expect(didSleep).toBe(false); // assert that sleep function hasn't resolved yet |
| 59 | + test('when promise rejects then it logs', (done) => { |
| 60 | + const logger = createLogger('async:utils'); |
20 | 61 |
|
21 |
| - jest.advanceTimersByTime(1000); // advance time for sleep function |
22 |
| - await jest.runAllTimersAsync(); // yield to let sleep function resolve |
| 62 | + runInBackground(async () => { |
| 63 | + throw new Error('test'); |
| 64 | + }); |
23 | 65 |
|
24 |
| - expect(didSleep).toBe(true); // assert that sleep function resolved |
| 66 | + setTimeout(() => { |
| 67 | + expect(logger.error).toHaveBeenCalledWith( |
| 68 | + 'unhandled promise exception: test', |
| 69 | + expect.any(Object) |
| 70 | + ); |
| 71 | + done(); |
| 72 | + }, 250); |
25 | 73 | });
|
26 | 74 | });
|
27 | 75 | });
|
0 commit comments