/
command.ts
68 lines (58 loc) 路 2.26 KB
/
command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import fixtures from "fixturez";
import fs from "fs-extra";
import path from "path";
import { defaultWrittenConfig } from "@changesets/config";
import { silenceLogsInBlock } from "@changesets/test-utils";
import initializeCommand from "..";
const f = fixtures(__dirname);
const getPaths = (cwd: string) => ({
readmePath: path.join(cwd, ".changeset/README.md"),
configPath: path.join(cwd, ".changeset/config.json"),
});
describe("init", () => {
silenceLogsInBlock();
it("should initialize in a project without a .changeset folder", async () => {
const cwd = await f.copy("without-existing-changeset");
const { readmePath, configPath } = getPaths(cwd);
expect(fs.pathExistsSync(readmePath)).toBe(false);
expect(fs.pathExistsSync(configPath)).toBe(false);
await initializeCommand(cwd);
expect(fs.pathExistsSync(readmePath)).toBe(true);
expect(fs.pathExistsSync(configPath)).toBe(true);
});
it("should write the default config if it doesn't exist", async () => {
const cwd = await f.copy("simple-project");
await fs.remove(path.join(cwd, ".changeset/config.json"));
expect(fs.pathExistsSync(path.join(cwd, ".changeset/README.md"))).toBe(
true
);
await initializeCommand(cwd);
expect(await fs.readJson(path.join(cwd, ".changeset/config.json"))).toEqual(
{ ...defaultWrittenConfig, baseBranch: "main" }
);
});
it("should add newline at the end of config", async () => {
const cwd = await f.copy("simple-project");
await fs.remove(path.join(cwd, ".changeset/config.json"));
await initializeCommand(cwd);
const configPath = path.join(cwd, ".changeset/config.json");
const config = (await fs.promises.readFile(configPath)).toString();
const lastCharacter = config.slice(-1);
expect(lastCharacter).toBe("\n");
});
it("shouldn't overwrite a config if it does exist", async () => {
const cwd = await f.copy("simple-project");
await fs.writeJson(path.join(cwd, ".changeset/config.json"), {
changelog: false,
});
expect(fs.pathExistsSync(path.join(cwd, ".changeset/README.md"))).toBe(
true
);
await initializeCommand(cwd);
expect(await fs.readJson(path.join(cwd, ".changeset/config.json"))).toEqual(
{
changelog: false,
}
);
});
});