1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
const test = require('tape');
const delay = require('delay');
const PCancelable = require('p-cancelable');
const inRange = require('in-range');
const timeSpan = require('time-span');
const pTimeout = require('.');
const fixture = Symbol('fixture');
const fixtureError = new Error('fixture');
test('resolves before timeout', async t => {
t.is(await pTimeout(delay(50).then(() => fixture), 200), fixture);
t.end();
});
test('handles milliseconds being `Infinity`', async t => {
t.is(
await pTimeout(delay(50, {value: fixture}), Infinity),
fixture
);
t.end();
});
test('accepts `customTimers` option', async t => {
t.plan(2);
await pTimeout(delay(50), 123, undefined, {
customTimers: {
setTimeout(fn, milliseconds) {
t.is(milliseconds, 123);
return setTimeout(fn, milliseconds);
},
clearTimeout(timeoutId) {
t.pass();
return clearTimeout(timeoutId);
}
}
});
t.end();
});
test('`.clear()` method', async t => {
const end = timeSpan();
const promise = pTimeout(delay(300), 200);
promise.clear();
await promise;
t.true(inRange(end(), {start: 0, end: 350}));
t.end();
});
|