|
| 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 | +}) |
0 commit comments