p-timeout/test.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-10-21 06:42:17 +00:00
import test from 'ava';
import delay from 'delay';
import PCancelable from 'p-cancelable';
import inRange from 'in-range';
import timeSpan from 'time-span';
2021-04-06 17:38:12 +00:00
import pTimeout, {TimeoutError} from './index.js';
2016-10-21 06:42:17 +00:00
const fixture = Symbol('fixture');
2019-03-12 08:21:47 +00:00
const fixtureError = new Error('fixture');
2016-10-21 06:42:17 +00:00
test('resolves before timeout', async t => {
t.is(await pTimeout(delay(50).then(() => fixture), {milliseconds: 200}), fixture);
2016-10-21 06:42:17 +00:00
});
2019-03-12 08:21:47 +00:00
test('throws when milliseconds is not number', async t => {
await t.throwsAsync(pTimeout(delay(50), {milliseconds: '200'}), {instanceOf: TypeError});
2017-11-19 09:27:16 +00:00
});
2019-03-12 08:21:47 +00:00
test('throws when milliseconds is negative number', async t => {
await t.throwsAsync(pTimeout(delay(50), {milliseconds: -1}), {instanceOf: TypeError});
2017-11-19 09:27:16 +00:00
});
test('throws when milliseconds is NaN', async t => {
await t.throwsAsync(pTimeout(delay(50), {milliseconds: Number.NaN}), {instanceOf: TypeError});
});
test('handles milliseconds being `Infinity`', async t => {
t.is(
await pTimeout(delay(50, {value: fixture}), {milliseconds: Number.POSITIVE_INFINITY}),
2022-07-25 16:24:32 +00:00
fixture,
);
});
2016-10-21 06:42:17 +00:00
test('rejects after timeout', async t => {
await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50}), {instanceOf: TimeoutError});
2016-10-21 06:42:17 +00:00
});
2022-11-25 08:54:50 +00:00
test('resolves after timeout with message:false', async t => {
t.is(
await pTimeout(delay(200), {milliseconds: 50, message: false}),
undefined,
);
});
2016-10-21 06:42:17 +00:00
test('rejects before timeout if specified promise rejects', async t => {
await t.throwsAsync(pTimeout(delay(50).then(() => {
throw fixtureError;
}), {milliseconds: 200}), {message: fixtureError.message});
2016-10-21 06:42:17 +00:00
});
test('fallback argument', async t => {
await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, message: 'rainbow'}), {message: 'rainbow'});
await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, message: new RangeError('cake')}), {instanceOf: RangeError});
await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, fallback: () => Promise.reject(fixtureError)}), {message: fixtureError.message});
await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, fallback() {
2017-11-28 20:05:54 +00:00
throw new RangeError('cake');
}}), {instanceOf: RangeError});
2016-10-21 06:42:17 +00:00
});
test('calls `.cancel()` on promise when it exists', async t => {
const promise = new PCancelable(async (resolve, reject, onCancel) => {
onCancel(() => {
t.pass();
});
await delay(200);
resolve();
});
await t.throwsAsync(pTimeout(promise, {milliseconds: 50}), {instanceOf: TimeoutError});
t.true(promise.isCanceled);
});
test('accepts `customTimers` option', async t => {
t.plan(2);
await pTimeout(delay(50), {
milliseconds: 123,
customTimers: {
setTimeout(fn, milliseconds) {
t.is(milliseconds, 123);
return setTimeout(fn, milliseconds);
},
clearTimeout(timeoutId) {
t.pass();
return clearTimeout(timeoutId);
2022-07-25 16:24:32 +00:00
},
},
});
});
test('`.clear()` method', async t => {
const end = timeSpan();
const promise = pTimeout(delay(300), {milliseconds: 200});
promise.clear();
await promise;
t.true(inRange(end(), {start: 0, end: 350}));
});
/**
TODO: Remove if statement when targeting Node.js 16.
*/
if (globalThis.AbortController !== undefined) {
test('rejects when calling `AbortController#abort()`', async t => {
const abortController = new AbortController();
const promise = pTimeout(delay(3000), {
milliseconds: 2000,
2022-07-25 16:24:32 +00:00
signal: abortController.signal,
});
abortController.abort();
await t.throwsAsync(promise, {
2022-07-25 16:24:32 +00:00
name: 'AbortError',
});
});
test('already aborted signal', async t => {
const abortController = new AbortController();
abortController.abort();
await t.throwsAsync(pTimeout(delay(3000), {
milliseconds: 2000,
2022-07-25 16:24:32 +00:00
signal: abortController.signal,
}), {
2022-07-25 16:24:32 +00:00
name: 'AbortError',
});
});
}