Skip to content

Commit

Permalink
refactor(publish): add common version command utils test fixture (#3638)
Browse files Browse the repository at this point in the history
refactor: add general version command utils test fixture
  • Loading branch information
amorscher committed Apr 11, 2023
1 parent 71a4cdd commit d407aaf
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 40 deletions.
16 changes: 16 additions & 0 deletions libs/commands/publish/__fixtures__/lerna-version-mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Creates and initializes needed version mocks for all publish specs
* @returns all created version mocks
*/
export function setupLernaVersionMocks() {
return {
gitPush: jest.mock("@lerna/commands/version/lib/git-push"),
isAnythingCommitted: jest.mock("@lerna/commands/version/lib/is-anything-committed", () => ({
isAnythingCommitted: jest.fn().mockResolvedValue(true),
})),
isBehindUpstream: jest.mock("@lerna/commands/version/lib/is-behind-upstream"),
remoteBranchExists: jest.mock("@lerna/commands/version/lib/remote-branch-exists", () => ({
remoteBranchExists: jest.fn().mockResolvedValue(true),
})),
};
}
10 changes: 2 additions & 8 deletions libs/commands/publish/src/lib/publish-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { commandRunner, commitChangeToPackage, initFixtureFactory, loggingOutput
import fsmain from "fs";
import fs from "fs-extra";
import path from "path";
import { setupLernaVersionMocks } from "../../__fixtures__/lerna-version-mocks";

// eslint-disable-next-line jest/no-mocks-import
jest.mock("@lerna/core", () => require("@lerna/test-helpers/__mocks__/@lerna/core"));
Expand All @@ -31,14 +32,7 @@ jest.mock("./get-unpublished-packages", () => ({
jest.mock("./git-checkout");

// lerna version mocks
jest.mock("@lerna/commands/version/lib/git-push");
jest.mock("@lerna/commands/version/lib/is-anything-committed", () => ({
isAnythingCommitted: jest.fn().mockResolvedValue(true),
}));
jest.mock("@lerna/commands/version/lib/is-behind-upstream");
jest.mock("@lerna/commands/version/lib/remote-branch-exists", () => ({
remoteBranchExists: jest.fn().mockResolvedValue(true),
}));
setupLernaVersionMocks();

const promptConfirmation = jest.mocked(_promptConfirmation);
const getOneTimePassword = jest.mocked(_getOneTimePassword);
Expand Down
10 changes: 2 additions & 8 deletions libs/commands/publish/src/lib/publish-licenses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { packDirectory as _packDirectory } from "@lerna/core";
import { commandRunner, initFixtureFactory, loggingOutput } from "@lerna/test-helpers";
import fs from "fs-extra";
import path from "path";
import { setupLernaVersionMocks } from "../../__fixtures__/lerna-version-mocks";

// eslint-disable-next-line jest/no-mocks-import
jest.mock("@lerna/core", () => require("@lerna/test-helpers/__mocks__/@lerna/core"));
Expand All @@ -18,14 +19,7 @@ jest.mock("./remove-temp-licenses", () => ({
}));

// lerna version mocks
jest.mock("@lerna/commands/version/lib/git-push");
jest.mock("@lerna/commands/version/lib/is-anything-committed", () => ({
isAnythingCommitted: jest.fn().mockResolvedValue(true),
}));
jest.mock("@lerna/commands/version/lib/is-behind-upstream");
jest.mock("@lerna/commands/version/lib/remote-branch-exists", () => ({
remoteBranchExists: jest.fn().mockResolvedValue(true),
}));
setupLernaVersionMocks();

// The mock differs from the real thing
const packDirectory = _packDirectory as any;
Expand Down
10 changes: 2 additions & 8 deletions libs/commands/publish/src/lib/publish-lifecycle-scripts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { packDirectory as _packDirectory, runLifecycle as _runLifecycle } from "
import { commandRunner, initFixtureFactory } from "@lerna/test-helpers";
import _loadJsonFile from "load-json-file";
import path from "path";
import { setupLernaVersionMocks } from "../../__fixtures__/lerna-version-mocks";

// eslint-disable-next-line jest/no-mocks-import
jest.mock("load-json-file", () => require("@lerna/test-helpers/__mocks__/load-json-file"));
Expand All @@ -20,14 +21,7 @@ jest.mock("./get-npm-username");
jest.mock("./get-two-factor-auth-required");

// lerna version mocks
jest.mock("@lerna/commands/version/lib/git-push");
jest.mock("@lerna/commands/version/lib/is-anything-committed", () => ({
isAnythingCommitted: jest.fn().mockResolvedValue(true),
}));
jest.mock("@lerna/commands/version/lib/is-behind-upstream");
jest.mock("@lerna/commands/version/lib/remote-branch-exists", () => ({
remoteBranchExists: jest.fn().mockResolvedValue(true),
}));
setupLernaVersionMocks();

// The mock differs from the real thing
const loadJsonFile = _loadJsonFile as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { commandRunner, gitAdd, gitCommit, gitTag, initFixtureFactory } from "@l
import fs from "fs-extra";
import path from "path";
import _writePkg from "write-pkg";
import { setupLernaVersionMocks } from "../../__fixtures__/lerna-version-mocks";

// eslint-disable-next-line jest/no-mocks-import
jest.mock("write-pkg", () => require("@lerna/test-helpers/__mocks__/write-pkg"));
Expand All @@ -27,14 +28,7 @@ jest.mock("./get-npm-username");
jest.mock("./get-two-factor-auth-required");

// lerna version mocks
jest.mock("@lerna/commands/version/lib/git-push");
jest.mock("@lerna/commands/version/lib/is-anything-committed", () => ({
isAnythingCommitted: jest.fn().mockResolvedValue(true),
}));
jest.mock("@lerna/commands/version/lib/is-behind-upstream");
jest.mock("@lerna/commands/version/lib/remote-branch-exists", () => ({
remoteBranchExists: jest.fn().mockResolvedValue(true),
}));
setupLernaVersionMocks();

// The mock differs from the real thing
const writePkg = _writePkg as any;
Expand Down
10 changes: 2 additions & 8 deletions libs/commands/publish/src/lib/publish-tagging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
npmPublish as _npmPublish,
} from "@lerna/core";
import { commandRunner, initFixtureFactory } from "@lerna/test-helpers";
import { setupLernaVersionMocks } from "../../__fixtures__/lerna-version-mocks";

// eslint-disable-next-line jest/no-mocks-import
jest.mock("@lerna/core", () => require("@lerna/test-helpers/__mocks__/@lerna/core"));
Expand All @@ -19,14 +20,7 @@ jest.mock("./get-npm-username");
jest.mock("./get-two-factor-auth-required");

// lerna version mocks
jest.mock("@lerna/commands/version/lib/git-push");
jest.mock("@lerna/commands/version/lib/is-anything-committed", () => ({
isAnythingCommitted: jest.fn().mockResolvedValue(true),
}));
jest.mock("@lerna/commands/version/lib/is-behind-upstream");
jest.mock("@lerna/commands/version/lib/remote-branch-exists", () => ({
remoteBranchExists: jest.fn().mockResolvedValue(true),
}));
setupLernaVersionMocks();

const npmDistTag = jest.mocked(_npmDistTag);

Expand Down

0 comments on commit d407aaf

Please sign in to comment.