Skip to content

Commit

Permalink
Use type-check to throw error instead of a broad try/catch
Browse files Browse the repository at this point in the history
Signed-off-by: Dinika Saxena <[email protected]>
  • Loading branch information
Dinika committed Nov 28, 2024
1 parent 28c801c commit c370222
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 12 additions & 12 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,18 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
// 4. we can then reapply the values after yargs-parser is done.
const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce(
(acc, arg) => {
try {
const pair = arg.split('=');
let flag = pair[0];
if (isNodeFlag(flag, false)) {
flag = flag.replace(/^--?/, '');
return arg.includes('=')
? acc.concat([[flag, pair[1]]])
: acc.concat([[flag, true]]);
}
return acc;
} catch (err) {
if (typeof arg !== 'string') {
throw new Error(`Invalid option ${arg} passed to mocha cli`);
}
const pair = arg.split('=');
let flag = pair[0];
if (isNodeFlag(flag, false)) {
flag = flag.replace(/^--?/, '');
return arg.includes('=')
? acc.concat([[flag, pair[1]]])
: acc.concat([[flag, true]]);
}
return acc;
},
[]
);
Expand Down Expand Up @@ -196,7 +195,8 @@ const loadPkgRc = (args = {}) => {
filepath
);
} else {
debug('failed to read default package.json at %s; ignoring', filepath);
debug('failed to read default package.json at %s; ignoring',
filepath);
return result;
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ describe('options', function () {
const filepath = '/some/package.json';
readFileSync = sinon.stub();
// package.json
readFileSync.onFirstCall().returns('{definitely-invalid');
readFileSync
.onFirstCall()
.returns('{definitely-invalid');
findConfig = sinon.stub().returns('/some/.mocharc.json');
loadConfig = sinon.stub().returns({});
findupSync = sinon.stub().returns(filepath);
Expand All @@ -222,7 +224,7 @@ describe('options', function () {
loadOptions();
},
'to throw',
/SyntaxError/
/SyntaxError/,
);
});
});
Expand Down

0 comments on commit c370222

Please sign in to comment.