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

Commit 49924ff

Browse files
committed
test: adds npm commands helper file
1 parent 5551955 commit 49924ff

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

__tests__/increment.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path"
44
import { runIncrement } from "../bin/increment.js"
55
import { pkgReporter, exitWithError } from "../bin/helpers/reporter.js"
66
import { cmd } from "../bin/helpers/cmd.js"
7+
import { getVersionCommand } from "../bin/helpers/npm-commands.js"
78

89
jest.mock("../bin/helpers/cmd.js", () => ({
910
cmd: jest.fn(),
@@ -55,12 +56,12 @@ describe("runIncrement()", () => {
5556
})
5657

5758
afterEach(() => {
58-
fs.writeFileSync.mockReset()
59+
fs.writeFileSync.mockRestore()
5960
})
6061

6162
it("versions an entry", async () => {
6263
// Given
63-
const command = `npm version -w ${entryOne.name} ${config.releaseVersion} --no-git-tag-version`
64+
const command = getVersionCommand(entryOne.name, config.releaseVersion)
6465
// When
6566
await runIncrement(config, entryOne)
6667
// Then
@@ -136,7 +137,7 @@ describe("runIncrement()", () => {
136137
})
137138

138139
afterEach(() => {
139-
fs.writeFileSync.mockReset()
140+
fs.writeFileSync.mockRestore()
140141
})
141142

142143
it("exits if write to file fails", async () => {

__tests__/publish.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getAddCommand } from "../bin/helpers/git-commands.js"
44
import { runPublish } from "../bin/publish.js"
55
import { cmd } from "../bin/helpers/cmd.js"
66
import { pkgReporter } from "../bin/helpers/reporter.js"
7+
import { getPublishCommand } from "../bin/helpers/npm-commands.js"
78

89
jest.mock("../bin/helpers/cmd.js", () => ({
910
cmd: jest.fn(),
@@ -57,7 +58,7 @@ describe("runPublish()", () => {
5758
await runPublish(baseConfig, entry)
5859
// Then
5960
expect(cmd).toBeCalledWith(
60-
`npm publish -w ${entry.name} --tag latest`,
61+
getPublishCommand(entry.name, "latest"),
6162
baseConfig,
6263
pkgReporter
6364
)
@@ -80,7 +81,7 @@ describe("runPublish()", () => {
8081
await runPublish(config, entry)
8182
// Then
8283
expect(cmd).toBeCalledWith(
83-
`npm publish -w ${entry.name} --tag next`,
84+
getPublishCommand(entry.name, "next"),
8485
config,
8586
pkgReporter
8687
)
@@ -96,7 +97,7 @@ describe("runPublish()", () => {
9697
await runPublish(config, entry)
9798
// Then
9899
expect(cmd).toBeCalledWith(
99-
`npm publish -w ${entry.name} --tag alpha`,
100+
getPublishCommand(entry.name, "alpha"),
100101
config,
101102
pkgReporter
102103
)
@@ -112,7 +113,7 @@ describe("runPublish()", () => {
112113
await runPublish(config, entry)
113114
// Then
114115
expect(cmd).toBeCalledWith(
115-
`npm publish -w ${entry.name} --tag beta`,
116+
getPublishCommand(entry.name, "beta"),
116117
config,
117118
pkgReporter
118119
)

bin/helpers/npm-commands.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function getPublishCommand(name, tag) {
2+
return `npm publish -w ${name} --tag ${tag}`
3+
}
4+
5+
export function getVersionCommand(name, version) {
6+
return `npm version -w ${name} ${version} --no-git-tag-version`
7+
}

bin/increment.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "path"
55
import { exitWithError, pkgReporter } from "./helpers/reporter.js"
66
import { ROOT_PACKAGE_FILE } from "./helpers/constants.js"
77
import { cmd } from "./helpers/cmd.js"
8+
import { getVersionCommand } from "./helpers/npm-commands.js"
89

910
function getSemverRangePart(version) {
1011
let rangePart = ""
@@ -88,9 +89,11 @@ function setDependencies(config, entry) {
8889
export async function runIncrement(config, entry) {
8990
pkgReporter.start("Version")
9091

91-
const incCommand = `npm version -w ${entry.name} ${config.releaseVersion} --no-git-tag-version`
92-
93-
await cmd(incCommand, config, pkgReporter)
92+
await cmd(
93+
getVersionCommand(entry.name, config.releaseVersion),
94+
config,
95+
pkgReporter
96+
)
9497

9598
setDependencies(config, entry)
9699

bin/publish.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import semver from "semver"
44
import { getAddCommand } from "./helpers/git-commands.js"
55
import { cmd } from "./helpers/cmd.js"
66
import { pkgReporter } from "./helpers/reporter.js"
7+
import { getPublishCommand } from "./helpers/npm-commands.js"
78

89
function parsePreId(version) {
910
const parts = semver.prerelease(version) || []
@@ -21,10 +22,8 @@ export async function runPublish(config, entry) {
2122
"latest"
2223

2324
if (!isPrivate) {
24-
const pubCommand = `npm publish -w ${entry.name} --tag ${tag}`
25-
2625
await cmd(getAddCommand(), config, pkgReporter)
27-
await cmd(pubCommand, config, pkgReporter)
26+
await cmd(getPublishCommand(entry.name, tag), config, pkgReporter)
2827

2928
pkgReporter.succeed("Publish successful")
3029
} else {

0 commit comments

Comments
 (0)