Skip to content

Commit

Permalink
100% code coverage (#53)
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
ameech authored and sindresorhus committed Mar 12, 2019
1 parent 781731b commit 37c1204
Showing 1 changed file with 113 additions and 1 deletion.
114 changes: 113 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ test('.add() - concurrency: 5', async t => {
test('.add() - priority', async t => {
const result = [];
const queue = new PQueue({concurrency: 1});
queue.add(async () => result.push(1), {priority: 1});
queue.add(async () => result.push(0), {priority: 0});
queue.add(async () => result.push(1), {priority: 1});
queue.add(async () => result.push(2), {priority: 1});
queue.add(async () => result.push(3), {priority: 2});
queue.add(async () => result.push(0), {priority: -1});
await queue.onEmpty();
t.deepEqual(result, [0, 3, 1, 2]);
t.deepEqual(result, [1, 3, 1, 2, 0, 0]);
});

test('.onEmpty()', async t => {
Expand Down Expand Up @@ -114,6 +116,17 @@ test('.onIdle()', async t => {
t.is(queue.pending, 0);
});

test('.onIdle() - no pending', async t => {
const queue = new PQueue();

t.is(queue.size, 0);
t.is(queue.pending, 0);

const p = await queue.onIdle();

t.is(p, undefined);
});

test('.clear()', t => {
const queue = new PQueue({concurrency: 2});
queue.add(() => delay(20000));
Expand Down Expand Up @@ -158,6 +171,49 @@ test('enforce number in options.concurrency', t => {
/* eslint-enable no-new */
});

test('enforce number in options.intervalCap', t => {
/* eslint-disable no-new */
t.throws(() => {
new PQueue({intervalCap: 0});
}, TypeError);
t.throws(() => {
new PQueue({intervalCap: undefined});
}, TypeError);
t.notThrows(() => {
new PQueue({intervalCap: 1});
});
t.notThrows(() => {
new PQueue({intervalCap: 10});
});
t.notThrows(() => {
new PQueue({intervalCap: Infinity});
});
/* eslint-enable no-new */
});

test('enforce finite in options.interval', t => {
/* eslint-disable no-new */
t.throws(() => {
new PQueue({interval: -1});
}, TypeError);
t.throws(() => {
new PQueue({interval: undefined});
}, TypeError);
t.throws(() => {
new PQueue({interval: Infinity});
});
t.notThrows(() => {
new PQueue({interval: 0});
});
t.notThrows(() => {
new PQueue({interval: 10});
});
t.throws(() => {
new PQueue({interval: Infinity});
});
/* eslint-enable no-new */
});

test('autoStart: false', t => {
const queue = new PQueue({concurrency: 2, autoStart: false});

Expand All @@ -178,6 +234,16 @@ test('autoStart: false', t => {
t.is(queue.size, 0);
});

test('.start() - not paused', t => {
const queue = new PQueue();

t.falsy(queue.isPaused);

queue.start();

t.falsy(queue.isPaused);
});

test('.pause()', t => {
const queue = new PQueue({concurrency: 2});

Expand Down Expand Up @@ -224,6 +290,36 @@ test('.add() sync/async mixed tasks', async t => {
t.is(queue.pending, 0);
});

test('.add() - handle task throwing error', async t => {
const queue = new PQueue({concurrency: 1});

queue.add(() => 'sync 1');
t.throwsAsync(queue.add(() => {
throw new Error('broken');
}), 'broken');
queue.add(() => 'sync 2');

t.is(queue.size, 2);

await queue.onIdle();
});

test('.add() - handle task promise failure', async t => {
const queue = new PQueue({concurrency: 1});

t.throwsAsync(queue.add(async () => {
throw new Error('broken');
}), 'broken');

queue.add(() => 'task #1');

t.is(queue.pending, 1);

await queue.onIdle();

t.is(queue.pending, 0);
});

test('.addAll() sync/async mixed tasks', async t => {
const queue = new PQueue();
const fns = [
Expand Down Expand Up @@ -404,6 +500,22 @@ test('pause should work when throttled', async t => {
await delay(2500);
});

test('clear interval on pause', async t => {
const queue = new PQueue({
interval: 100,
intervalCap: 1
});

queue.add(() => {
queue.pause();
});
queue.add(() => 'task #1');

await delay(300);

t.is(queue.size, 1);
});

test('should be an event emitter', t => {
const queue = new PQueue();
t.true(queue instanceof EventEmitter);
Expand Down

0 comments on commit 37c1204

Please sign in to comment.