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 3 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
30 changes: 22 additions & 8 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,36 @@ const loadPkgRc = (args = {}) => {
result = {};
const filepath = args.package || findUp.sync(mocharc.package);
if (filepath) {
let configData;
try {
const pkg = JSON.parse(fs.readFileSync(filepath, 'utf8'));
configData = fs.readFileSync(filepath, 'utf8');
} catch (err) {
// If `args.package` was explicitly specified, throw an error
if (filepath == args.package) {
throw createUnparsableFileError(
`Unable to read ${filepath}: ${err}`,
filepath
);
} else {
debug('failed to read default package.json at %s; ignoring',
filepath);
return result;
}
}
try {
const pkg = JSON.parse(configData);
if (pkg.mocha) {
debug('`mocha` prop of package.json parsed: %O', pkg.mocha);
result = pkg.mocha;
} else {
debug('no config found in %s', filepath);
}
} catch (err) {
if (args.package) {
throw createUnparsableFileError(
`Unable to read/parse ${filepath}: ${err}`,
filepath
);
}
debug('failed to read default package.json at %s; ignoring', filepath);
// If JSON failed to parse, throw an error.
throw createUnparsableFileError(
`Unable to parse ${filepath}: ${err}`,
filepath
);
}
}
return result;
Expand Down
35 changes: 33 additions & 2 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('options', function () {
loadOptions('--package /something/wherever --require butts');
},
'to throw',
'Unable to read/parse /something/wherever: bad file message'
'Unable to read /something/wherever: bad file message'
);
});
});
Expand Down 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