@applicaster/zapplicaster-cli
Version:
CLI Tool for the zapp app and Quick Brick project
200 lines (157 loc) • 5.96 kB
JavaScript
const mock_publish_error = new Error("Failed to publish package");
jest.doMock("../../../shell", () => ({
runInShellAsync: jest.fn((command) => {
return command.includes("fail")
? Promise.reject(mock_publish_error)
: Promise.resolve();
}),
}));
jest.doMock("../../../logger", () => ({
log: jest.fn(),
warn: jest.fn(),
}));
jest.doMock("../../utils/wait", () => ({
wait: jest.fn(() => Promise.resolve(false)),
}));
jest.doMock("../../utils/isPackagePublished", () => ({
isPackagePublished: jest.fn(() => Promise.resolve(false)),
}));
jest.doMock("../../utils/retryPublishCommand", () => ({
retryPublishCommand: jest.fn((command) => {
return command.includes("fail")
? Promise.reject(mock_publish_error)
: Promise.resolve();
}),
}));
const getConfig = (conf = {}) => ({
dryRun: false,
yarn: false,
next: false,
version: "1.5.0",
pluginPath: "plugins/my-plugin",
...conf,
});
const { npmPublish } = require("../npmPublish");
const { runInShellAsync } = require("../../../shell");
const logger = require("../../../logger");
const { retryPublishCommand } = require("../../utils/retryPublishCommand");
function clearMocks() {
logger.log.mockClear();
retryPublishCommand.mockClear();
runInShellAsync.mockClear();
jest.resetModules();
}
describe("npm publish step", () => {
describe("with default options", () => {
beforeEach(clearMocks);
it("publishes the plugin to npm", async () => {
const configuration = getConfig();
expect(await npmPublish(configuration)).toEqual(true);
expect(retryPublishCommand).toHaveBeenCalledTimes(1);
expect(runInShellAsync).toHaveBeenCalledTimes(1);
expect(runInShellAsync).toHaveBeenNthCalledWith(
1,
`npm version ${configuration.version}`,
expect.objectContaining({ cwd: configuration.pluginPath })
);
expect(retryPublishCommand).toHaveBeenNthCalledWith(
1,
"npm publish",
expect.objectContaining({ cwd: configuration.pluginPath }),
configuration.version
);
});
it("throws if the npm command fails", async () => {
const configuration = getConfig({ version: "fail" });
expect(npmPublish(configuration)).rejects.toMatchSnapshot();
});
it.skip("re tries 3 times if the npm command fails with ESOCKETTIMEDOUT", async () => {
const configuration = getConfig({ yarn: true });
runInShellAsync.mockRejectedValue("ESOCKETTIMEDOUT");
try {
await npmPublish(configuration);
} catch (e) {
// Handle the error
expect(e).toBe("ESOCKETTIMEDOUT");
}
expect(runInShellAsync).toHaveBeenCalledTimes(3);
});
it.skip("it succeeds if the error is indicating that package was already published", async () => {
const configuration = getConfig({ yarn: true });
runInShellAsync.mockRejectedValue(
'"You cannot publish over the previously published version"'
);
const result = await npmPublish(configuration);
expect(result).toBe(true);
expect(runInShellAsync).toHaveBeenCalledTimes(1);
});
});
describe("with yarn flag", () => {
beforeEach(clearMocks);
it("uses yarn instead of npm", async () => {
const configuration = getConfig({ yarn: true });
expect(await npmPublish(configuration)).toEqual(true);
expect(retryPublishCommand).toHaveBeenCalledTimes(1);
expect(retryPublishCommand).toHaveBeenCalledWith(
"npm publish",
expect.objectContaining({ cwd: configuration.pluginPath }),
configuration.version
);
});
});
describe("with dry run flag", () => {
beforeEach(clearMocks);
it("performs a dry run", async () => {
const configuration = getConfig({ dryRun: true });
expect(await npmPublish(configuration)).toEqual(true);
expect(runInShellAsync).toHaveBeenCalledTimes(1);
expect(retryPublishCommand).toHaveBeenCalledTimes(1);
expect(runInShellAsync).toHaveBeenNthCalledWith(
1,
`npm version ${configuration.version}`,
expect.objectContaining({ cwd: configuration.pluginPath })
);
expect(retryPublishCommand).toHaveBeenNthCalledWith(
1,
"npm publish --dry-run",
expect.objectContaining({ cwd: configuration.pluginPath }),
configuration.version
);
});
// didn't work
it.skip("throws if used with the yarn flag as well", async () => {
const configuration = getConfig({ dryRun: true, yarn: true });
const res = await npmPublish(configuration);
expect(res).toMatchSnapshot();
});
});
describe("with next flag", () => {
beforeEach(clearMocks);
it("adds the next flag in the cli command", async () => {
const configuration = getConfig({ next: true });
expect(await npmPublish(configuration)).toEqual(true);
expect(runInShellAsync).toHaveBeenCalledTimes(1);
expect(retryPublishCommand).toHaveBeenCalledTimes(1);
expect(runInShellAsync).toHaveBeenNthCalledWith(
1,
`npm version ${configuration.version}`,
expect.objectContaining({ cwd: configuration.pluginPath })
);
expect(retryPublishCommand).toHaveBeenNthCalledWith(
1,
"npm publish --tag next",
expect.objectContaining({ cwd: configuration.pluginPath }),
configuration.version
);
});
it("adds the next flag when using yarn as well", async () => {
const configuration = getConfig({ next: true, yarn: true });
expect(await npmPublish(configuration)).toEqual(true);
expect(retryPublishCommand).toHaveBeenCalledTimes(1);
expect(runInShellAsync).toHaveBeenCalledWith(
`npm version ${configuration.version} --git-tag-version=false --commit-hooks=false --workspaces-update=false`,
expect.objectContaining({ cwd: configuration.pluginPath })
);
});
});
});