Skip to content

Commit

Permalink
fix(project): Report syntax errors in root package.json
Browse files Browse the repository at this point in the history
Fixes #1452
  • Loading branch information
evocateur committed Jun 8, 2018
1 parent 1f885f1 commit f674f35
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
17 changes: 17 additions & 0 deletions core/command/__tests__/command.test.js
Expand Up @@ -448,6 +448,23 @@ describe("core-command", () => {
}
});

it("throws JSONError when root package.json has syntax error", async () => {
expect.assertions(1);

const cwd = await initFixture("basic");

await fs.writeFile(
path.join(cwd, "package.json"), // trailing comma ...v
'{ "name": "invalid", "lerna": { "version": "1.0.0" }, }'
);

try {
await testFactory({ cwd });
} catch (err) {
expect(err.prefix).toBe("JSONError");
}
});

it("throws ENOLERNA when lerna.json is not found", async () => {
expect.assertions(1);

Expand Down
14 changes: 14 additions & 0 deletions core/project/__fixtures__/pkg-prop-syntax-error/package.json
@@ -0,0 +1,14 @@
{
"name": "pkg-prop-syntax-error",
"version": "0.0.0-root",
"private": true,
"lerna": {
"loglevel": "success",
"command": {
"publish": {
"loglevel": "verbose"
}
},
"version": "1.0.0"
},,
}
13 changes: 13 additions & 0 deletions core/project/__tests__/project.test.js
Expand Up @@ -85,6 +85,19 @@ describe("Project", () => {
});
});

it("errors when root package.json is not valid JSON", async () => {
expect.assertions(2);

const cwd = await initFixture("pkg-prop-syntax-error");

try {
const project = new Project(cwd); // eslint-disable-line no-unused-vars
} catch (err) {
expect(err.name).toBe("ValidationError");
expect(err.prefix).toBe("JSONError");
}
});

it("extends local shared config", async () => {
const cwd = await initFixture("extends");
const project = new Project(cwd);
Expand Down
6 changes: 5 additions & 1 deletion core/project/index.js
Expand Up @@ -111,7 +111,11 @@ class Project {
value: manifest,
});
} catch (err) {
// syntax errors are already caught and reported by constructor
// redecorate JSON syntax errors, avoid debug dump
if (err.name === "JSONError") {
throw new ValidationError(err.name, err.message);
}

// try again next time
}

Expand Down

0 comments on commit f674f35

Please sign in to comment.