Skip to content

Commit

Permalink
Add another test
Browse files Browse the repository at this point in the history
Fixes #57
  • Loading branch information
sindresorhus committed Mar 7, 2022
1 parent 3b62341 commit 735d80e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -5,10 +5,10 @@ export default async function pMap(
mapper,
{
concurrency = Number.POSITIVE_INFINITY,
stopOnError = true
} = {}
stopOnError = true,
} = {},
) {
return new Promise((resolve, reject_) => { // eslint-disable-line promise/param-names
return new Promise((resolve, reject_) => {
if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
}
Expand Down Expand Up @@ -64,7 +64,7 @@ export default async function pMap(

isResolved = true;

if (!skippedIndexesMap.size) {
if (skippedIndexesMap.size === 0) {
resolve(result);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions index.test-d.ts
Expand Up @@ -4,13 +4,13 @@ import pMap, {Options, Mapper, pMapSkip} from './index.js';
const sites = [
'https://sindresorhus.com',
'https://avajs.dev',
'https://github.com'
'https://github.com',
];

const numbers = [
0,
1,
2
2,
];

const asyncMapper = async (site: string): Promise<string> => site;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -44,12 +44,12 @@
"aggregate-error": "^4.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"ava": "^4.1.0",
"delay": "^5.0.0",
"in-range": "^3.0.0",
"random-int": "^3.0.0",
"time-span": "^5.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"tsd": "^0.19.1",
"xo": "^0.48.0"
}
}
2 changes: 1 addition & 1 deletion test-multiple-pmapskips-performance.js
Expand Up @@ -13,7 +13,7 @@ function generateSkipPerformanceData(length) {
}

test('multiple pMapSkips - algorithmic complexity', async t => {
const testData = [generateSkipPerformanceData(1000), generateSkipPerformanceData(10000), generateSkipPerformanceData(100000)];
const testData = [generateSkipPerformanceData(1000), generateSkipPerformanceData(10_000), generateSkipPerformanceData(100_000)];
const testDurationsMS = [];

for (const data of testData) {
Expand Down
40 changes: 22 additions & 18 deletions test.js
Expand Up @@ -9,7 +9,7 @@ import pMap, {pMapSkip} from './index.js';
const sharedInput = [
[async () => 10, 300],
[20, 200],
[30, 100]
[30, 100],
];

const errorInput1 = [
Expand All @@ -20,7 +20,7 @@ const errorInput1 = [
}, 10],
[() => {
throw new Error('bar');
}, 10]
}, 10],
];

const errorInput2 = [
Expand All @@ -31,7 +31,7 @@ const errorInput2 = [
[30, 100],
[() => {
throw new Error('foo');
}, 10]
}, 10],
];

const mapper = async ([value, ms]) => {
Expand Down Expand Up @@ -72,7 +72,7 @@ class ThrowingIterator {
// eslint is wrong - bind is needed else the next() call cannot update
// this.index, which we need to track how many times the iterator was called
// eslint-disable-next-line no-extra-bind
}).bind(this)
}).bind(this),
};
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ test('pMapSkip', async t => {
t.deepEqual(await pMap([
1,
pMapSkip,
2
2,
], async value => value), [1, 2]);
});

Expand All @@ -164,7 +164,7 @@ test('multiple pMapSkips', async t => {
3,
pMapSkip,
pMapSkip,
4
4,
], async value => value), [1, 2, 3, 4]);
});

Expand All @@ -173,7 +173,7 @@ test('all pMapSkips', async t => {
pMapSkip,
pMapSkip,
pMapSkip,
pMapSkip
pMapSkip,
], async value => value), []);
});

Expand All @@ -188,7 +188,7 @@ test('all mappers should run when concurrency is infinite, even after stop-on-er
await delay(100);
throw new Error('Oops!');
}
})
}),
);
await delay(500);
t.deepEqual(mappedValues, [1, 3, 2]);
Expand Down Expand Up @@ -287,7 +287,7 @@ test('asyncIterator - pMapSkip', async t => {
t.deepEqual(await pMap(new AsyncTestData([
1,
pMapSkip,
2
2,
]), async value => value), [1, 2]);
});

Expand All @@ -300,7 +300,7 @@ test('asyncIterator - multiple pMapSkips', async t => {
3,
pMapSkip,
pMapSkip,
4
4,
]), async value => value), [1, 2, 3, 4]);
});

Expand All @@ -309,7 +309,7 @@ test('asyncIterator - all pMapSkips', async t => {
pMapSkip,
pMapSkip,
pMapSkip,
pMapSkip
pMapSkip,
]), async value => value), []);
});

Expand All @@ -328,7 +328,7 @@ test('asyncIterator - all mappers should run when concurrency is infinite, even
throw new Error(`Oops! ${value}`);
}
}),
{message: 'Oops! 1'}
{message: 'Oops! 1'},
);
await delay(500);
t.deepEqual(mappedValues, [1, 3, 2]);
Expand All @@ -344,7 +344,7 @@ test('catches exception from source iterator - 1st item', async t => {
await delay(100);
return value;
},
{concurrency: 1, stopOnError: true}
{concurrency: 1, stopOnError: true},
));
t.is(error.message, 'throwing on index 0');
t.is(input.index, 1);
Expand All @@ -365,7 +365,7 @@ test('catches exception from source iterator - 2nd item', async t => {
await delay(100);
return value;
},
{concurrency: 1, stopOnError: true}
{concurrency: 1, stopOnError: true},
));
await delay(300);
t.is(input.index, 2);
Expand All @@ -384,7 +384,7 @@ test('catches exception from source iterator - 2nd item after 1st item mapper th
await delay(100);
throw new Error('mapper threw error');
},
{concurrency: 1, stopOnError: false}
{concurrency: 1, stopOnError: false},
));
await delay(300);
t.is(error.message, 'throwing on index 1');
Expand Down Expand Up @@ -414,7 +414,7 @@ test('asyncIterator - get the correct exception after stop-on-error', async t =>
test('incorrect input type', async t => {
let mapperCalled = false;

const task = pMap(123456, async () => {
const task = pMap(123_456, async () => {
mapperCalled = true;
await delay(100);
});
Expand All @@ -432,7 +432,7 @@ test('no unhandled rejected promises from mapper throws - infinite concurrency',
await delay(100);
throw new Error(`Oops! ${value}`);
}),
{message: 'Oops! 1'}
{message: 'Oops! 1'},
);
// Note: All 3 mappers get invoked, all 3 throw, even with `{stopOnError: true}` this
// should raise an AggregateError with all 3 exceptions instead of throwing 1
Expand All @@ -450,7 +450,11 @@ test('no unhandled rejected promises from mapper throws - concurrency 1', async
throw new Error(`Oops! ${value}`);
},
{concurrency: 1}),
{message: 'Oops! 1'}
{message: 'Oops! 1'},
);
t.deepEqual(mappedValues, [1]);
});

test('invalid mapper', async t => {
await t.throwsAsync(pMap([], 'invalid mapper', {concurrency: 2}), {instanceOf: TypeError});
});

0 comments on commit 735d80e

Please sign in to comment.