Skip to content

Commit a5f686a

Browse files
authored
Merge pull request #8 from vltansky/tests
tests: throw errors & -r=7 & unexpected error
2 parents 67dd516 + dbb272b commit a5f686a

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

cli.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ module.exports = async () => {
2424
}catch(err){
2525
await fs.remove(tempDir);
2626
if(err.code === 'ETARGET'){
27-
spinner.fail(chalk.red(`version '${err.wanted}' not found in npm registry\navailable versions:`));
28-
console.log(err.versions.reverse().join(' | '));
29-
process.exit(1);
30-
}/* istanbul ignore next */else{
31-
spinner.fail('Unexpected error');
32-
console.error(err);
33-
process.exit(-1);
34-
};
35-
return;
27+
const msg = chalk.red(`version '${err.wanted}' not found in npm registry\navailable versions:\n`)
28+
spinner.fail(msg+err.versions.reverse().join(' | '));
29+
throw err.code;
30+
}
31+
spinner.fail('Unexpected error');
32+
throw new Error(err);
3633
}
3734
await fs.copy(tempDir+'/dist', targetDir);
3835
await fs.remove(tempDir);

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('./cli')();
1+
require('./cli')().catch(console.error);

tests/test.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const all_versions = [
2424
const cases = [
2525
null,
2626
"-r=latest",
27+
"-r=7",
2728
"-r=7.2.0",
2829
"-r=v7.2.0",
2930
"-r=v7.2",
@@ -112,13 +113,26 @@ describe.each(cases)("Downloading %s", (version) => {
112113

113114
describe("Errors", () => {
114115
test("Wrong version 6..2.3", async () => {//maybe create test.each() for more errors scenarios
115-
const mockExit = jest.spyOn(process, "exit").mockImplementation(() => {});
116116
const version = "-r=6..2.3";
117+
try{
118+
await runCli(version);
119+
}catch(err){
120+
expect(err).toBe('ETARGET');
121+
}finally{
122+
await fs.remove(versionFolder(version));
123+
}
124+
});
125+
});
117126

118-
await runCli(version);
119-
120-
await fs.remove(versionFolder(version));
121-
expect(mockExit).toHaveBeenCalledWith(1);
122-
mockExit.mockRestore();
127+
describe("Unexpected errors", () => {
128+
test("Unexpected error 6..2.3,7.2.3", async () => {//maybe create test.each() for more errors scenarios
129+
const version = "-r=6..2.3,7.2.3";
130+
try{
131+
await runCli(version);
132+
}catch(err){
133+
expect(err).not.toBe('ETARGET');
134+
}finally{
135+
await fs.remove(versionFolder(version));
136+
}
123137
});
124138
});

0 commit comments

Comments
 (0)