p-timeout/test.js

50 lines
1.3 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';
2017-07-02 01:43:15 +00:00
import m from '.';
2016-10-21 06:42:17 +00:00
const fixture = Symbol('fixture');
const fixtureErr = new Error('fixture');
test('resolves before timeout', async t => {
t.is(await m(delay(50).then(() => fixture), 200), fixture);
});
2017-11-19 09:27:16 +00:00
test('throws when ms is not number', async t => {
await t.throws(m(delay(50), '200'), TypeError);
});
test('throws when ms is negative number', async t => {
await t.throws(m(delay(50), -1), TypeError);
});
2016-10-21 06:42:17 +00:00
test('rejects after timeout', async t => {
await t.throws(m(delay(200), 50), m.TimeoutError);
});
test('rejects before timeout if specified promise rejects', async t => {
await t.throws(m(delay(50).then(() => Promise.reject(fixtureErr)), 200), fixtureErr.message);
});
test('fallback argument', async t => {
await t.throws(m(delay(200), 50, 'rainbow'), 'rainbow');
await t.throws(m(delay(200), 50, new RangeError('cake')), RangeError);
await t.throws(m(delay(200), 50, () => Promise.reject(fixtureErr)), fixtureErr.message);
2017-11-28 20:05:54 +00:00
await t.throws(m(delay(200), 50, () => {
throw new RangeError('cake');
}), RangeError);
2016-10-21 06:42:17 +00:00
});
test('calls `.cancel()` on promise when it exists', async t => {
const p = new PCancelable(onCancel => {
onCancel(() => {
t.pass();
});
return delay(200);
});
await t.throws(m(p, 50), m.TimeoutError);
t.true(p.canceled);
});