2021-04-06 17:38:12 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
2020-12-01 15:40:53 +00:00
|
|
|
import {expectType, expectError} from 'tsd';
|
2021-04-06 17:38:12 +00:00
|
|
|
import pTimeout, {TimeoutError} from './index.js';
|
2019-03-12 08:17:11 +00:00
|
|
|
|
2022-07-25 16:16:27 +00:00
|
|
|
const delayedPromise: () => Promise<string> = async () => new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve('foo');
|
|
|
|
}, 200);
|
2019-03-12 08:17:11 +00:00
|
|
|
});
|
2022-07-25 16:16:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2022-11-25 09:02:37 +00:00
|
|
|
pTimeout(delayedPromise(), {milliseconds: 50, message: false}).then(value => {
|
|
|
|
expectType<string>(value);
|
|
|
|
});
|
2022-07-25 16:24:32 +00:00
|
|
|
pTimeout(delayedPromise(), {milliseconds: 50, message: new Error('error')}).then(value => {
|
|
|
|
expectType<string>(value);
|
|
|
|
});
|
2022-07-25 16:16:27 +00:00
|
|
|
pTimeout(delayedPromise(), {milliseconds: 50, fallback: async () => 10}).then(value => {
|
2019-03-12 08:17:11 +00:00
|
|
|
expectType<string | number>(value);
|
|
|
|
});
|
2022-07-25 16:16:27 +00:00
|
|
|
pTimeout(delayedPromise(), {milliseconds: 50, fallback: () => 10}).then(value => {
|
2019-03-12 08:17:11 +00:00
|
|
|
expectType<string | number>(value);
|
|
|
|
});
|
|
|
|
|
2020-12-01 15:40:53 +00:00
|
|
|
const customTimers = {setTimeout, clearTimeout};
|
2022-07-25 16:16:27 +00:00
|
|
|
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});
|
2020-12-01 15:40:53 +00:00
|
|
|
|
2022-07-25 16:16:27 +00:00
|
|
|
expectError(pTimeout(delayedPromise(), {
|
|
|
|
milliseconds: 50,
|
|
|
|
fallback: () => 10,
|
|
|
|
customTimers: {
|
2022-07-25 16:24:32 +00:00
|
|
|
setTimeout,
|
|
|
|
},
|
2022-07-25 16:16:27 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
expectError(pTimeout(delayedPromise(), {
|
|
|
|
milliseconds: 50,
|
|
|
|
fallback: () => 10,
|
2020-12-01 15:40:53 +00:00
|
|
|
customTimers: {
|
|
|
|
setTimeout: () => 42, // Invalid `setTimeout` implementation
|
2022-07-25 16:24:32 +00:00
|
|
|
clearTimeout,
|
|
|
|
},
|
2020-12-01 15:40:53 +00:00
|
|
|
}));
|
|
|
|
|
2022-07-25 16:16:27 +00:00
|
|
|
expectError(pTimeout(delayedPromise(), {})); // `milliseconds` is required
|
|
|
|
|
2019-04-04 04:58:41 +00:00
|
|
|
const timeoutError = new TimeoutError();
|
|
|
|
expectType<TimeoutError>(timeoutError);
|