Fix value check logic for ms arg (#5)

This commit is contained in:
Neek Sandhu 2017-11-19 01:27:16 -08:00 committed by Sindre Sorhus
parent 339b0ac5db
commit 1a651607e1
2 changed files with 9 additions and 1 deletions

View File

@ -9,7 +9,7 @@ class TimeoutError extends Error {
} }
module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => { module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => {
if (typeof ms !== 'number' && ms >= 0) { if (typeof ms !== 'number' || ms < 0) {
throw new TypeError('Expected `ms` to be a positive number'); throw new TypeError('Expected `ms` to be a positive number');
} }

View File

@ -9,6 +9,14 @@ test('resolves before timeout', async t => {
t.is(await m(delay(50).then(() => fixture), 200), fixture); t.is(await m(delay(50).then(() => fixture), 200), fixture);
}); });
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);
});
test('rejects after timeout', async t => { test('rejects after timeout', async t => {
await t.throws(m(delay(200), 50), m.TimeoutError); await t.throws(m(delay(200), 50), m.TimeoutError);
}); });