-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from akshay5995/test/add-more-ut
refactor: add more unit tests
- Loading branch information
Showing
11 changed files
with
265 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const SINCE_MOCK = "commit-sha"; | ||
const MOCK_CHANGED_PACAKGES = [{ name: "root", version: "0.0.1" }]; | ||
const MOCK_OUTPUT = JSON.stringify(MOCK_CHANGED_PACAKGES); | ||
|
||
const execMock = jest.fn().mockImplementation((cmd, callback) => { | ||
callback(null, MOCK_OUTPUT); | ||
}); | ||
|
||
jest.mock("child_process", () => ({ | ||
exec: execMock, | ||
})); | ||
|
||
import getChangedPackages from "./getChangedPackages"; | ||
|
||
describe("getChangedPackages", () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("should call the right methods and return right result", async () => { | ||
const result = await getChangedPackages(SINCE_MOCK); | ||
expect(execMock).toBeCalledTimes(1); | ||
expect(execMock).toBeCalledWith( | ||
`pnpm --filter "...[${SINCE_MOCK}]" list --json --depth=-1`, | ||
expect.anything() | ||
); | ||
expect(result).toStrictEqual(MOCK_CHANGED_PACAKGES); | ||
}); | ||
|
||
test("should fail if pnpm command fails", async () => { | ||
const FAKE_ERROR = Error("Cmd: falied due to some reason"); | ||
execMock.mockImplementation((cmd, callback) => { | ||
callback(FAKE_ERROR, null); | ||
}); | ||
|
||
await expect(getChangedPackages(SINCE_MOCK)).rejects.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const PACKAGE_NAME_MOCKED = "root"; | ||
|
||
const readPackageUpMock = jest.fn(); | ||
|
||
jest.mock("read-pkg-up", () => ({ | ||
readPackageUp: readPackageUpMock, | ||
})); | ||
|
||
import getRootPackageName from "./getRootPackageName"; | ||
|
||
describe("getRootPackageName", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("should call the right method", async () => { | ||
readPackageUpMock.mockResolvedValue({ | ||
packageJson: { | ||
name: PACKAGE_NAME_MOCKED, | ||
}, | ||
}); | ||
const result = await getRootPackageName(); | ||
expect(readPackageUpMock).toBeCalledTimes(1); | ||
expect(result).toBe(PACKAGE_NAME_MOCKED); | ||
}); | ||
|
||
test("throw error when root package couldn't be found", async () => { | ||
readPackageUpMock.mockResolvedValue(null); | ||
await expect(getRootPackageName()).rejects.toThrow( | ||
"Couldn't read root package.json" | ||
); | ||
expect(readPackageUpMock).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import getChangedPackages from "./getChangedPackages"; | ||
import getRootPackageName from "./getRootPackageName"; | ||
|
||
export { getChangedPackages, getRootPackageName }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { packageName, since } from "./inputs"; | ||
import { | ||
setEnvChangeDetected, | ||
outputChangedPackages, | ||
setFailedMessage, | ||
} from "./outputs"; | ||
|
||
export { | ||
packageName, | ||
since, | ||
setEnvChangeDetected, | ||
outputChangedPackages, | ||
setFailedMessage, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,3 @@ | ||
import { setFailed } from "@actions/core"; | ||
import getChangedPackages from "./core/getChangedPackages"; | ||
import getRootPackageName from "./core/getRootPackageName"; | ||
import { packageName, since } from "./helpers/inputs"; | ||
import { setEnvChangeDetected, outputChangedPackages } from "./helpers/ouputs"; | ||
import run from "./run"; | ||
|
||
const main = async () => { | ||
try { | ||
const changedPackages = await getChangedPackages(since); | ||
const packageNames = changedPackages.map((p) => p.name); | ||
const rootPackage = await getRootPackageName(); | ||
|
||
const hasRootOrGivenPackageChanged = | ||
packageNames.includes(rootPackage) || packageNames.includes(packageName); | ||
|
||
setEnvChangeDetected(hasRootOrGivenPackageChanged); | ||
outputChangedPackages(packageNames); | ||
} catch (error) { | ||
setFailed((error as Error).message); | ||
} | ||
}; | ||
|
||
main(); | ||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const PACKAGE_NAME_MOCKED = "fake-package"; | ||
const SINCE_MOCKED = "commit-sha"; | ||
|
||
const setFailedMessageMock = jest.fn(); | ||
const outputChangedPackagesMock = jest.fn(); | ||
const setEnvChangeDetectedMock = jest.fn(); | ||
const getChangedPackagesMock = jest.fn().mockReturnValue([ | ||
{ name: "root", version: "0.0.1" }, | ||
{ name: "some-fake-package", version: "0.0.1" }, | ||
]); | ||
const getRootPackageNameMock = jest.fn().mockReturnValue("root"); | ||
|
||
jest.mock("./helpers", () => ({ | ||
setEnvChangeDetected: setEnvChangeDetectedMock, | ||
outputChangedPackages: outputChangedPackagesMock, | ||
packageName: PACKAGE_NAME_MOCKED, | ||
since: SINCE_MOCKED, | ||
setFailedMessage: setFailedMessageMock, | ||
})); | ||
|
||
jest.mock("./core", () => ({ | ||
getRootPackageName: getRootPackageNameMock, | ||
getChangedPackages: getChangedPackagesMock, | ||
})); | ||
|
||
import run from "./run"; | ||
|
||
describe("run", () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("should call all the right methods", async () => { | ||
await run(); | ||
expect(getChangedPackagesMock).toBeCalledTimes(1); | ||
expect(getChangedPackagesMock).toBeCalledWith(SINCE_MOCKED); | ||
expect(getRootPackageNameMock).toBeCalledTimes(1); | ||
expect(setEnvChangeDetectedMock).toBeCalledTimes(1); | ||
expect(setEnvChangeDetectedMock).toBeCalledWith(true); | ||
expect(outputChangedPackagesMock).toBeCalledTimes(1); | ||
expect(outputChangedPackagesMock).toBeCalledWith([ | ||
"root", | ||
"some-fake-package", | ||
]); | ||
}); | ||
|
||
test("should call setFailed when there's an error", async () => { | ||
const FAKE_ERROR = Error("Action failed due to error"); | ||
getChangedPackagesMock.mockRejectedValue(FAKE_ERROR); | ||
|
||
await expect(run()).resolves.not.toThrow(); | ||
expect(getChangedPackagesMock).toBeCalledTimes(1); | ||
expect(getChangedPackagesMock).toBeCalledWith(SINCE_MOCKED); | ||
expect(getRootPackageNameMock).not.toBeCalled(); | ||
expect(setEnvChangeDetectedMock).not.toBeCalled(); | ||
expect(outputChangedPackagesMock).not.toBeCalled(); | ||
expect(setFailedMessageMock).toBeCalledTimes(1); | ||
expect(setFailedMessageMock).toBeCalledWith(FAKE_ERROR); | ||
}); | ||
}); |
Oops, something went wrong.