Skip to content
This repository was archived by the owner on Jun 3, 2023. It is now read-only.

Commit 5551955

Browse files
committed
test: adds publish tests
1 parent 9bb457b commit 5551955

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

__tests__/publish.spec.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import "./mocks.js"
2+
import path from "path"
3+
import { getAddCommand } from "../bin/helpers/git-commands.js"
4+
import { runPublish } from "../bin/publish.js"
5+
import { cmd } from "../bin/helpers/cmd.js"
6+
import { pkgReporter } from "../bin/helpers/reporter.js"
7+
8+
jest.mock("../bin/helpers/cmd.js", () => ({
9+
cmd: jest.fn(),
10+
}))
11+
12+
jest.mock("../bin/helpers/reporter.js", () => ({
13+
pkgReporter: {
14+
start: jest.fn(),
15+
succeed: jest.fn(),
16+
},
17+
}))
18+
19+
const entry = {
20+
name: "@test/one",
21+
dir: path.resolve(process.cwd(), "packages/one"),
22+
getPackage() {
23+
return {
24+
name: "@test/one",
25+
version: "0.0.1",
26+
}
27+
},
28+
}
29+
30+
const privateEntry = {
31+
name: "@test/two",
32+
dir: path.resolve(process.cwd(), "packages/two"),
33+
getPackage() {
34+
return {
35+
name: "@test/two",
36+
private: true,
37+
version: "0.0.1",
38+
}
39+
},
40+
}
41+
42+
const baseConfig = {
43+
npmTag: "",
44+
preid: "",
45+
}
46+
47+
describe("runPublish()", () => {
48+
it("adds changes to stage", async () => {
49+
// When
50+
await runPublish(baseConfig, entry)
51+
// Then
52+
expect(cmd).toBeCalledWith(getAddCommand(), baseConfig, pkgReporter)
53+
})
54+
55+
it("publishes package", async () => {
56+
// When
57+
await runPublish(baseConfig, entry)
58+
// Then
59+
expect(cmd).toBeCalledWith(
60+
`npm publish -w ${entry.name} --tag latest`,
61+
baseConfig,
62+
pkgReporter
63+
)
64+
})
65+
66+
it("does not publish private package", async () => {
67+
// When
68+
await runPublish(baseConfig, privateEntry)
69+
// Then
70+
expect(cmd).not.toBeCalled()
71+
})
72+
73+
it("publishes with npm tag if given", async () => {
74+
// Given
75+
const config = {
76+
...baseConfig,
77+
npmTag: "next",
78+
}
79+
// When
80+
await runPublish(config, entry)
81+
// Then
82+
expect(cmd).toBeCalledWith(
83+
`npm publish -w ${entry.name} --tag next`,
84+
config,
85+
pkgReporter
86+
)
87+
})
88+
89+
it("publishes with preid if given", async () => {
90+
// Given
91+
const config = {
92+
...baseConfig,
93+
preid: "alpha",
94+
}
95+
// When
96+
await runPublish(config, entry)
97+
// Then
98+
expect(cmd).toBeCalledWith(
99+
`npm publish -w ${entry.name} --tag alpha`,
100+
config,
101+
pkgReporter
102+
)
103+
})
104+
105+
it("publishes with parsed preid if it exists", async () => {
106+
// Given
107+
const config = {
108+
...baseConfig,
109+
releaseVersion: "0.0.1-beta.0",
110+
}
111+
// When
112+
await runPublish(config, entry)
113+
// Then
114+
expect(cmd).toBeCalledWith(
115+
`npm publish -w ${entry.name} --tag beta`,
116+
config,
117+
pkgReporter
118+
)
119+
})
120+
})

bin/helpers/git-commands.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export function getTagCmd(msg, version) {
99
export function getPushCmd() {
1010
return "git push --follow-tags"
1111
}
12+
13+
export function getAddCommand() {
14+
return "git add . -u"
15+
}

bin/publish.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
import semver from "semver"
4+
import { getAddCommand } from "./helpers/git-commands.js"
45
import { cmd } from "./helpers/cmd.js"
56
import { pkgReporter } from "./helpers/reporter.js"
67

@@ -20,10 +21,9 @@ export async function runPublish(config, entry) {
2021
"latest"
2122

2223
if (!isPrivate) {
23-
const addChangesCommand = "git add . -u"
2424
const pubCommand = `npm publish -w ${entry.name} --tag ${tag}`
2525

26-
await cmd(addChangesCommand, config, pkgReporter)
26+
await cmd(getAddCommand(), config, pkgReporter)
2727
await cmd(pubCommand, config, pkgReporter)
2828

2929
pkgReporter.succeed("Publish successful")

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"scripts": {
1111
"prepare": "husky install",
1212
"test": "jest",
13+
"test:watch": "jest --watch",
1314
"test:rw": "npm run test:increment && npm run test:clear-tag",
1415
"test:increment": "release-workspaces -i patch --git.commitMessage 'chore: test release ${version}'",
1516
"test:clear-tag": "git push --delete origin $(git describe --abbrev=0) && git tag -d $(git describe --abbrev=0)",

0 commit comments

Comments
 (0)