|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const { suite, test } = require('node:test'); |
| 4 | + |
| 5 | +suite('input validation', () => { |
| 6 | + test('throws if condition is not a function', (t) => { |
| 7 | + t.assert.throws(() => { |
| 8 | + t.waitFor(5); |
| 9 | + }, { |
| 10 | + code: 'ERR_INVALID_ARG_TYPE', |
| 11 | + message: /The "condition" argument must be of type function/, |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + test('throws if options is not an object', (t) => { |
| 16 | + t.assert.throws(() => { |
| 17 | + t.waitFor(() => {}, null); |
| 18 | + }, { |
| 19 | + code: 'ERR_INVALID_ARG_TYPE', |
| 20 | + message: /The "options" argument must be of type object/, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + test('throws if options.interval is not a number', (t) => { |
| 25 | + t.assert.throws(() => { |
| 26 | + t.waitFor(() => {}, { interval: 'foo' }); |
| 27 | + }, { |
| 28 | + code: 'ERR_INVALID_ARG_TYPE', |
| 29 | + message: /The "options\.interval" property must be of type number/, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + test('throws if options.timeout is not a number', (t) => { |
| 34 | + t.assert.throws(() => { |
| 35 | + t.waitFor(() => {}, { timeout: 'foo' }); |
| 36 | + }, { |
| 37 | + code: 'ERR_INVALID_ARG_TYPE', |
| 38 | + message: /The "options\.timeout" property must be of type number/, |
| 39 | + }); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +test('returns the result of the condition function', async (t) => { |
| 44 | + const result = await t.waitFor(() => { |
| 45 | + return 42; |
| 46 | + }); |
| 47 | + |
| 48 | + t.assert.strictEqual(result, 42); |
| 49 | +}); |
| 50 | + |
| 51 | +test('returns the result of an async condition function', async (t) => { |
| 52 | + const result = await t.waitFor(async () => { |
| 53 | + return 84; |
| 54 | + }); |
| 55 | + |
| 56 | + t.assert.strictEqual(result, 84); |
| 57 | +}); |
| 58 | + |
| 59 | +test('errors if the condition times out', async (t) => { |
| 60 | + await t.assert.rejects(async () => { |
| 61 | + await t.waitFor(() => { |
| 62 | + return new Promise(() => {}); |
| 63 | + }, { |
| 64 | + interval: 60_000, |
| 65 | + timeout: 1, |
| 66 | + }); |
| 67 | + }, { |
| 68 | + message: /waitFor\(\) timed out/, |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +test('polls until the condition returns successfully', async (t) => { |
| 73 | + let count = 0; |
| 74 | + const result = await t.waitFor(() => { |
| 75 | + ++count; |
| 76 | + if (count < 4) { |
| 77 | + throw new Error('resource is not ready yet'); |
| 78 | + } |
| 79 | + |
| 80 | + return 'success'; |
| 81 | + }, { |
| 82 | + interval: 1, |
| 83 | + timeout: 60_000, |
| 84 | + }); |
| 85 | + |
| 86 | + t.assert.strictEqual(result, 'success'); |
| 87 | + t.assert.strictEqual(count, 4); |
| 88 | +}); |
| 89 | + |
| 90 | +test('sets last failure as error cause on timeouts', async (t) => { |
| 91 | + const error = new Error('boom'); |
| 92 | + await t.assert.rejects(async () => { |
| 93 | + await t.waitFor(() => { |
| 94 | + return new Promise((_, reject) => { |
| 95 | + reject(error); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }, (err) => { |
| 99 | + t.assert.match(err.message, /timed out/); |
| 100 | + t.assert.strictEqual(err.cause, error); |
| 101 | + return true; |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +test('limits polling if condition takes longer than interval', async (t) => { |
| 106 | + let count = 0; |
| 107 | + |
| 108 | + function condition() { |
| 109 | + count++; |
| 110 | + return new Promise((resolve) => { |
| 111 | + setTimeout(() => { |
| 112 | + resolve('success'); |
| 113 | + }, 200); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + const result = await t.waitFor(condition, { |
| 118 | + interval: 1, |
| 119 | + timeout: 60_000, |
| 120 | + }); |
| 121 | + |
| 122 | + t.assert.strictEqual(result, 'success'); |
| 123 | + t.assert.strictEqual(count, 1); |
| 124 | +}); |
0 commit comments