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
|
|
|
|
2020-12-01 15:50:36 +00:00
|
|
|
const delayedPromise: () => Promise<string> = async () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve('foo');
|
|
|
|
}, 200);
|
|
|
|
});
|
|
|
|
};
|
2019-03-12 08:17:11 +00:00
|
|
|
|
|
|
|
pTimeout(delayedPromise(), 50).then(() => 'foo');
|
2021-04-06 17:38:12 +00:00
|
|
|
pTimeout(delayedPromise(), 50, async () => {
|
2019-03-12 08:17:11 +00:00
|
|
|
return pTimeout(delayedPromise(), 300);
|
|
|
|
});
|
|
|
|
pTimeout(delayedPromise(), 50).then(value => expectType<string>(value));
|
|
|
|
pTimeout(delayedPromise(), 50, 'error').then(value =>
|
|
|
|
expectType<string>(value)
|
|
|
|
);
|
|
|
|
pTimeout(delayedPromise(), 50, new Error('error')).then(value =>
|
|
|
|
expectType<string>(value)
|
|
|
|
);
|
|
|
|
pTimeout(delayedPromise(), 50, async () => 10).then(value => {
|
|
|
|
expectType<string | number>(value);
|
|
|
|
});
|
|
|
|
pTimeout(delayedPromise(), 50, () => 10).then(value => {
|
|
|
|
expectType<string | number>(value);
|
|
|
|
});
|
|
|
|
|
2020-12-01 15:40:53 +00:00
|
|
|
const customTimers = {setTimeout, clearTimeout};
|
|
|
|
pTimeout(delayedPromise(), 50, undefined, {customTimers});
|
|
|
|
pTimeout(delayedPromise(), 50, 'foo', {customTimers});
|
|
|
|
pTimeout(delayedPromise(), 50, new Error('error'), {customTimers});
|
|
|
|
pTimeout(delayedPromise(), 50, () => 10, {});
|
|
|
|
|
|
|
|
expectError(pTimeout(delayedPromise(), 50, () => 10, {customTimers: {setTimeout}}));
|
|
|
|
expectError(pTimeout(delayedPromise(), 50, () => 10, {
|
|
|
|
customTimers: {
|
|
|
|
setTimeout: () => 42, // Invalid `setTimeout` implementation
|
|
|
|
clearTimeout
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2019-04-04 04:58:41 +00:00
|
|
|
const timeoutError = new TimeoutError();
|
|
|
|
expectType<TimeoutError>(timeoutError);
|