Skip to content

Commit b9df596

Browse files
Prevent fatal errors when using the newly-introduced prettier option (changesets#1590)
* fix(config): validate prettier option * style: format * test(cli): prettier config * test(cli): prettier config * fix tests * Update .changeset/curly-cameras-serve.md --------- Co-authored-by: Mateusz Burzyński <[email protected]>
1 parent 1621d9d commit b9df596

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

.changeset/curly-cameras-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/config": patch
3+
---
4+
5+
Prevent fatal error when using the newly-introduced `prettier` option

packages/cli/src/run.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { error } from "@changesets/logger";
22
import { testdir } from "@changesets/test-utils";
33

44
import { run } from "./run";
5+
import writeChangeset from "@changesets/write";
56

67
jest.mock("@changesets/logger");
78
jest.mock("./commands/version");
@@ -168,5 +169,59 @@ describe("cli", () => {
168169
`It looks like you are trying to use the \`--ignore\` option while ignore is defined in the config file. This is currently not allowed, you can only use one of them at a time.`
169170
);
170171
});
172+
173+
it("should not throw if `prettier: false` is configured", async () => {
174+
const cwd = await testdir({
175+
"package.json": JSON.stringify({
176+
private: true,
177+
workspaces: ["packages/*"],
178+
}),
179+
"packages/pkg-a/package.json": JSON.stringify({
180+
name: "pkg-a",
181+
version: "1.0.0",
182+
}),
183+
".changeset/config.json": JSON.stringify({
184+
prettier: false,
185+
}),
186+
});
187+
await writeChangeset(
188+
{
189+
summary: "This is a summary",
190+
releases: [{ name: "pkg-a", type: "minor" }],
191+
},
192+
cwd
193+
);
194+
195+
await expect(run(["version"], {}, cwd)).resolves.not.toThrow();
196+
});
197+
198+
it('should throw if `prettier: "string"` is configured', async () => {
199+
const cwd = await testdir({
200+
"package.json": JSON.stringify({
201+
private: true,
202+
workspaces: ["packages/*"],
203+
}),
204+
"packages/pkg-a/package.json": JSON.stringify({
205+
name: "pkg-a",
206+
version: "1.0.0",
207+
}),
208+
".changeset/config.json": JSON.stringify({
209+
prettier: "no thanks",
210+
}),
211+
});
212+
await writeChangeset(
213+
{
214+
summary: "This is a summary",
215+
releases: [{ name: "pkg-a", type: "minor" }],
216+
},
217+
cwd
218+
);
219+
220+
await expect(run(["version"], {}, cwd)).rejects
221+
.toThrowErrorMatchingInlineSnapshot(`
222+
"Some errors occurred when validating the changesets config:
223+
The \`prettier\` option is set as "no thanks" when the only valid values are undefined or a boolean"
224+
`);
225+
});
171226
});
172227
});

packages/config/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export let parse = (json: WrittenConfig, packages: Packages): Config => {
331331
}
332332
}
333333

334-
if (json.prettier !== undefined) {
334+
if (json.prettier !== undefined && typeof json.prettier !== "boolean") {
335335
messages.push(
336336
`The \`prettier\` option is set as ${JSON.stringify(
337337
json.prettier,

0 commit comments

Comments
 (0)