Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle case of invalid package.json with no explicit config #5198

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')) {
dhdaines marked this conversation as resolved.
Show resolved Hide resolved
throw createUnparsableFileError(
dhdaines marked this conversation as resolved.
Show resolved Hide resolved
`Unable to parse ${filepath}: ${err}`,
filepath
);
}
debug('failed to read default package.json at %s; ignoring', filepath);
}
Expand Down
33 changes: 32 additions & 1 deletion 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"],}}');
dhdaines marked this conversation as resolved.
Show resolved Hide resolved
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 Expand Up @@ -287,7 +318,7 @@ describe('options', function () {
});

it('should set config = false', function () {
expect(loadOptions(), 'to have property', 'config', false);
expect(result, 'to have property', 'config', false);
});
});

Expand Down
Loading