Skip to content

Commit

Permalink
Integration test for --fail-fast and --serial 🛡️
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrieanKhisbe committed Jan 2, 2024
1 parent 5b18779 commit d07b5c2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const getPackages = async argv => {
const isSingleOutput = argv =>
_.some(opt => opt in argv, ['size', 'json', 'gzip-size', 'dependencies']);

const shouldStopOnError = (packages, argv) => _.size(packages) <= 1 || argv['fail-fast'] || false;

const main = async ({argv, stream = process.stdout}) => {
if ('range' in argv && 'r' in argv && argv._.length > 1)
throw new Error("Can't use both --range option and list of packages");
Expand Down Expand Up @@ -57,7 +59,7 @@ const main = async ({argv, stream = process.stdout}) => {
spinner.info(view(stats));
return stats;
},
{concurrency: argv.serial ? 1 : undefined, stopOnError: argv['fail-fast']}
{concurrency: argv.serial ? 1 : undefined, stopOnError: shouldStopOnError(packages, argv)}
).catch(err => {
spinner.stop();
throw err;
Expand Down
60 changes: 59 additions & 1 deletion test/integration/core.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,65 @@ test('fetch just a single package', async t => {
test('fetch just a list of package', async t => {
const stream = fakeStream();
// had to pin version for test stability
await main({argv: {_: ['lodash@2.4.2', 'moment@1.2.0']}, stream});
await main({argv: {_: ['lodash@2.4.2', 'moment@1.2.0'], serial: 1}, stream});
const EXPECT_TEXT = `- Fetching stats for package lodash@2.4.2
ℹ lodash (2.4.2) has 0 dependencies for a weight of 27.94KB (10.04KB gzipped)
- Fetching stats for package moment@1.2.0
ℹ moment (1.2.0) has 0 dependencies for a weight of 114.1KB (14.83KB gzipped)
ℹ total (2 packages) has 0 dependencies for a weight of 142.04KB (24.87KB gzipped)
`;

t.deepEqual(stream.getContent().split('\n').sort(), EXPECT_TEXT.split('\n').sort());
});

test('fetch all not stoping on error', async t => {
const stream = fakeStream();
// had to pin version for test stability
await t.throwsAsync(
() =>
main({
argv: {_: ['lodash@2.4.2', '@oh-no/no-noooo', 'moment@1.2.0'], serial: 1},
stream
}),
{message: /The package you were looking for doesn't exist./}
// TODO: enforce exact wording (and adequate formating)
);
t.is(
stream.getContent(),
`- Fetching stats for package lodash@2.4.2
ℹ lodash (2.4.2) has 0 dependencies for a weight of 27.94KB (10.04KB gzipped)
- Fetching stats for package @oh-no/no-noooo
✖ resolving @oh-no/no-noooo failed: The package you were looking for doesn't exist.
- Fetching stats for package moment@1.2.0
ℹ moment (1.2.0) has 0 dependencies for a weight of 114.1KB (14.83KB gzipped)
`
);
});

test('fetch all stoping on first error with flag', async t => {
const stream = fakeStream();
// had to pin version for test stability
const err = await main({
argv: {_: ['lodash@2.4.2', '@oh-no/no-noooo', 'moment@1.2.0'], serial: 1, 'fail-fast': true},
stream
}).catch(err => err);

t.is(
stream.getContent(),
`- Fetching stats for package lodash@2.4.2
ℹ lodash (2.4.2) has 0 dependencies for a weight of 27.94KB (10.04KB gzipped)
- Fetching stats for package @oh-no/no-noooo
✖ resolving @oh-no/no-noooo failed: The package you were looking for doesn't exist.
`
);
t.is(err.message, "The package you were looking for doesn't exist.");
});

test('fetch just a list of package serialy', async t => {
const stream = fakeStream();
// had to pin version for test stability
await main({argv: {_: ['lodash@2.4.2', 'moment@1.2.0'], serial: 1}, stream});

t.is(
stream.getContent(),
Expand Down

0 comments on commit d07b5c2

Please sign in to comment.