File: index.test-d.ts

package info (click to toggle)
node-p-timeout 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 164 kB
  • sloc: javascript: 258; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,455 bytes parent folder | download
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
import {expectType, expectError} from 'tsd';
import pTimeout = require('.');
import {TimeoutError} from '.';

const delayedPromise: () => Promise<string> = async () => {
	return new Promise(resolve => {
		setTimeout(() => {
			resolve('foo');
		}, 200);
	});
};

pTimeout(delayedPromise(), 50).then(() => 'foo');
pTimeout(delayedPromise(), 50, () => {
	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);
});

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
	}
}));

const timeoutError = new TimeoutError();
expectType<TimeoutError>(timeoutError);