| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import rejectAfterTimeout from '../rejectAfterTimeout'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | describe('rejectAfterTimeout', () => { |
| b69ab31 | | | 11 | beforeEach(() => { |
| b69ab31 | | | 12 | jest.useFakeTimers(); |
| b69ab31 | | | 13 | }); |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | test('instant promise should win', async () => { |
| b69ab31 | | | 16 | const instantPromise = Promise.resolve('winner'); |
| b69ab31 | | | 17 | const result = await rejectAfterTimeout(instantPromise, 5_000, 'too slow?'); |
| b69ab31 | | | 18 | expect(result).toBe('winner'); |
| b69ab31 | | | 19 | jest.advanceTimersByTime(5_000); |
| b69ab31 | | | 20 | }); |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | test('fast promise should win', async () => { |
| b69ab31 | | | 23 | const fastPromise = new Promise(resolve => setTimeout(() => resolve('winner'), 1_000)); |
| b69ab31 | | | 24 | const promise = rejectAfterTimeout(fastPromise, 5_000, 'too slow?'); |
| b69ab31 | | | 25 | jest.advanceTimersByTime(1_000); |
| b69ab31 | | | 26 | const result = await promise; |
| b69ab31 | | | 27 | expect(result).toBe('winner'); |
| b69ab31 | | | 28 | jest.advanceTimersByTime(4_000); |
| b69ab31 | | | 29 | }); |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | test('slow promise should lose', () => { |
| b69ab31 | | | 32 | const slowPromise = new Promise(resolve => setTimeout(() => resolve('winner'), 5_000)); |
| b69ab31 | | | 33 | const promise = rejectAfterTimeout(slowPromise, 1_000, 'too slow?'); |
| b69ab31 | | | 34 | jest.advanceTimersByTime(1_000); |
| b69ab31 | | | 35 | expect(promise).rejects.toBe('too slow?'); |
| b69ab31 | | | 36 | jest.advanceTimersByTime(4_000); |
| b69ab31 | | | 37 | }); |
| b69ab31 | | | 38 | }); |