Skip to content

Commit e55165e

Browse files
committed
test: version not found handling
1 parent 550068a commit e55165e

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

cli.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ module.exports = async () => {
2626
if(err.code === 'ETARGET'){
2727
spinner.fail(chalk.red(`version '${err.wanted}' not found in npm registry\navailable versions:`));
2828
console.log(err.versions.reverse().join(' | '));
29-
}else{
29+
process.exit(1);
30+
}/* istanbul ignore next */else{
3031
spinner.fail('Unexpected error');
3132
console.error(err);
32-
}
33-
process.exit(0);
33+
process.exit(-1);
34+
};
35+
return;
3436
}
3537
await fs.copy(tempDir+'/dist', targetDir);
3638
await fs.remove(tempDir);

tests/test.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,33 @@ const tempDir = os.tmpdir()+`/${packageName}-staging`;
99
// const { packument } = require('pacote');
1010
// const versions = await packument(packageName);
1111
// cases = ['', 'latest', '-r=7.3.0', ...Object.keys(versions)];
12-
const all_versions = ['0.0.1','5.3.0',
13-
'6.0.0', '6.0.1',
14-
'6.1.0', '7.0.0',
15-
'7.0.1', '7.1.0',
16-
'7.2.0', '7.3.0'];
12+
const all_versions = [
13+
'0.0.1',
14+
'5.3.0',
15+
'6.0.0',
16+
'6.0.1',
17+
'6.1.0',
18+
'7.0.0',
19+
'7.0.1',
20+
'7.1.0',
21+
'7.2.0',
22+
'7.3.0'
23+
];
1724
const cases = ['-r=7.2.0','-r=v7.2.0','-r=v7.2', '--release=7.3.0', ...all_versions.map(v=>'-r='+v)];
25+
26+
const runCli = async(version) => {//TODO: blank output
27+
process.argv.push(`./out/${version}`);
28+
if(version){
29+
process.argv.push(version)
30+
}
31+
await cli();
32+
process.argv = process.argv.filter(v=>v!==version && v !== `./out/${version}`);//revert process args
33+
}
1834
describe.each(cases)(
1935
"Downloading %s",
2036
(version) => {
2137
beforeAll(async () => {
22-
jest.spyOn(process, 'exit').mockImplementationOnce(() => {
23-
throw new Error('process.exit() was called.'+version)
24-
});
25-
process.argv.push(`./out/${version}`);
26-
if(version){
27-
process.argv.push(version)
28-
}
29-
await cli();
30-
process.argv = process.argv.filter(v=>v!==version && v !== `./out/${version}`);//revert process args
38+
await runCli(version);
3139
});
3240
afterAll(async () => {
3341
await fs.remove(`./out/${version}`);
@@ -62,11 +70,16 @@ describe.each(cases)(
6270
});
6371
}
6472
);
65-
// test('Attempt 1', async() => {
66-
// const spy = jest.spyOn(console, 'log');
6773

68-
// expect(() => {
69-
// }).toThrow('process.exit() was called.');
70-
// expect(spy.mock.calls).toEqual([['Testing...']]);
71-
// expect(process.exit).toHaveBeenCalledWith(0);
72-
// });
74+
describe("Errors", () => {
75+
test("wrong version 6..2.3", async()=>{
76+
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
77+
const version = '-r=6..2.3';
78+
79+
await runCli(version);
80+
81+
await fs.remove(`./out/${version}`);
82+
expect(mockExit).toHaveBeenCalledWith(1);
83+
mockExit.mockRestore();
84+
})
85+
});

0 commit comments

Comments
 (0)