@applicaster/zapplicaster-cli
Version:
CLI Tool for the zapp app and Quick Brick project
70 lines (50 loc) • 1.79 kB
JavaScript
const { runInShellAsync } = require("../../../shell");
const { retryPublishCommand } = require("../retryPublishCommand");
jest.mock("../../../shell", () => ({
runInShellAsync: jest.fn(),
}));
jest.mock("../isPackagePublished", () => ({
isPackagePublished: jest.fn(() => Promise.resolve(false)),
}));
jest.mock("../wait", () => ({
wait: jest.fn(() => Promise.resolve()),
}));
function clearMocks() {
runInShellAsync.mockClear();
jest.resetModules();
}
describe("retryPublishCommand", () => {
beforeEach(clearMocks);
it("re tries 3 times if the npm command fails with ESOCKETTIMEDOUT", async () => {
runInShellAsync.mockRejectedValue("ESOCKETTIMEDOUT");
try {
await retryPublishCommand("fail");
} catch (e) {
// Handle the error
expect(e).toBe("ESOCKETTIMEDOUT");
}
expect(runInShellAsync).toHaveBeenCalledTimes(3);
});
it("it succeeds if the error is indicating that package was already published", async () => {
runInShellAsync.mockRejectedValue(
'"You cannot publish over the previously published version"'
);
const result = await retryPublishCommand();
expect(result).toBe(true);
expect(runInShellAsync).toHaveBeenCalledTimes(1);
});
it("it succeeds if no errors are thrown from shell command", async () => {
runInShellAsync.mockResolvedValue(true);
const result = await retryPublishCommand();
expect(result).toBe(true);
expect(runInShellAsync).toHaveBeenCalledTimes(1);
});
it("Passes commands to shell script", async () => {
runInShellAsync.mockResolvedValue(true);
await retryPublishCommand("npm publish --tag next", "cmd options");
expect(runInShellAsync).toHaveBeenCalledWith(
"npm publish --tag next",
"cmd options"
);
});
});