p-timeout/index.test-d.ts

56 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-04-06 17:38:12 +00:00
/* eslint-disable @typescript-eslint/no-floating-promises */
import {expectType, expectError} from 'tsd';
2021-04-06 17:38:12 +00:00
import pTimeout, {TimeoutError} from './index.js';
const delayedPromise: () => Promise<string> = async () => new Promise(resolve => {
setTimeout(() => {
resolve('foo');
}, 200);
});
pTimeout(delayedPromise(), {milliseconds: 50}).then(() => 'foo');
pTimeout(delayedPromise(), {milliseconds: 50, fallback: async () => pTimeout(delayedPromise(), {milliseconds: 300})});
2022-07-25 16:24:32 +00:00
pTimeout(delayedPromise(), {milliseconds: 50}).then(value => {
expectType<string>(value);
});
pTimeout(delayedPromise(), {milliseconds: 50, message: 'error'}).then(value => {
expectType<string>(value);
});
pTimeout(delayedPromise(), {milliseconds: 50, message: new Error('error')}).then(value => {
expectType<string>(value);
});
pTimeout(delayedPromise(), {milliseconds: 50, fallback: async () => 10}).then(value => {
expectType<string | number>(value);
});
pTimeout(delayedPromise(), {milliseconds: 50, fallback: () => 10}).then(value => {
expectType<string | number>(value);
});
const customTimers = {setTimeout, clearTimeout};
pTimeout(delayedPromise(), {milliseconds: 50, customTimers});
pTimeout(delayedPromise(), {milliseconds: 50, message: 'foo', customTimers});
pTimeout(delayedPromise(), {milliseconds: 50, message: new Error('error'), customTimers});
pTimeout(delayedPromise(), {milliseconds: 50, fallback: () => 10});
expectError(pTimeout(delayedPromise(), {
milliseconds: 50,
fallback: () => 10,
customTimers: {
2022-07-25 16:24:32 +00:00
setTimeout,
},
}));
expectError(pTimeout(delayedPromise(), {
milliseconds: 50,
fallback: () => 10,
customTimers: {
setTimeout: () => 42, // Invalid `setTimeout` implementation
2022-07-25 16:24:32 +00:00
clearTimeout,
},
}));
expectError(pTimeout(delayedPromise(), {})); // `milliseconds` is required
const timeoutError = new TimeoutError();
expectType<TimeoutError>(timeoutError);