Skip to content

Commit

Permalink
fix: report syntax errors in package.json (fixes: mochajs#5141)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Aug 14, 2024
1 parent d5766c8 commit 6d8e90d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ const loadPkgRc = (args = {}) => {
`Unable to read/parse ${filepath}: ${err}`,
filepath
);
} else if (err.toString().includes('SyntaxError')) {
throw createUnparsableFileError(
`Unable to parse ${filepath}: ${err}`,
filepath
);
}
debug('failed to read default package.json at %s; ignoring', filepath);
}
Expand Down
31 changes: 31 additions & 0 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,37 @@ describe('options', function () {
});
});

describe('when path to package.json unspecified and package.json exists but is invalid', function () {
beforeEach(function () {
const filepath = '/some/package.json';
readFileSync = sinon.stub();
// package.json
readFileSync
.onFirstCall()
// Note the extra comma
.returns('{"mocha": {"retries": 3, "_": ["foobar.spec.js"],}}');
findConfig = sinon.stub().returns('/some/.mocharc.json');
loadConfig = sinon.stub().returns({});
findupSync = sinon.stub().returns(filepath);
loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});
});

it('should throw', function () {
expect(
() => {
loadOptions();
},
'to throw',
'Unable to parse /some/package.json: SyntaxError: Expected double-quoted property name in JSON at position 49'
);
});
});

describe('when called with package = false (`--no-package`)', function () {
let result;
beforeEach(function () {
Expand Down

0 comments on commit 6d8e90d

Please sign in to comment.