Skip to content

Commit

Permalink
test: utilize jest.mocked function
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Feb 20, 2024
1 parent 13e3269 commit b505cef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 56 deletions.
62 changes: 27 additions & 35 deletions src/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,30 @@ beforeEach(async () => {
jest.clearAllMocks();

logs = [];
(core.info as jest.Mock<typeof core.info>).mockImplementation((message) => {
jest.mocked(core.info).mockImplementation((message) => {
logs.push(message);
});
jest.mocked(core.warning).mockImplementation((message) => {
logs.push(message);
});
(core.warning as jest.Mock<typeof core.warning>).mockImplementation(
(message) => {
logs.push(message);
},
);
});

describe("Getting the cache key", () => {
beforeEach(async () => {
const { getYarnVersion } = await import("./yarn/index.js");

(getYarnVersion as jest.Mock<typeof getYarnVersion>).mockResolvedValue(
"1.2.3",
);
jest.mocked(getYarnVersion).mockResolvedValue("1.2.3");
});

it("should get the cache key", async () => {
const { hashFile } = await import("hasha");
const fs = (await import("node:fs")).default;
const { getCacheKey } = await import("./cache.js");

(fs.existsSync as jest.Mock<typeof fs.existsSync>).mockImplementation(
(path) => {
if (path == "yarn.lock") return true;
return false;
},
);
jest.mocked(fs.existsSync).mockImplementation((path) => {
if (path == "yarn.lock") return true;
return false;
});

(hashFile as jest.Mock).mockImplementation(async (filePath) => {
if (filePath == "yarn.lock") return "b1484caea0bbcbfa9a3a32591e3cad5d";
Expand All @@ -80,7 +74,7 @@ describe("Getting the cache key", () => {
const fs = (await import("node:fs")).default;
const { getCacheKey } = await import("./cache.js");

(fs.existsSync as jest.Mock<typeof fs.existsSync>).mockReturnValue(false);
jest.mocked(fs.existsSync).mockReturnValue(false);

const cacheKey = await getCacheKey();
const expectedCacheKey = `yarn-install-action-${os.type()}-1.2.3`;
Expand All @@ -99,25 +93,23 @@ it("should get the cache paths", async () => {
const { getYarnConfig } = await import("./yarn/index.js");
const { getCachePaths } = await import("./cache.js");

(getYarnConfig as jest.Mock<typeof getYarnConfig>).mockImplementation(
async (name) => {
switch (name) {
case "cacheFolder":
return ".yarn/cache";
case "deferredVersionFolder":
return ".yarn/versions";
case "installStatePath":
return ".yarn/install-state.gz";
case "patchFolder":
return ".yarn/patches";
case "pnpUnpluggedFolder":
return ".yarn/unplugged";
case "virtualFolder":
return ".yarn/__virtual__";
}
throw new Error(`unknown config: ${name}`);
},
);
jest.mocked(getYarnConfig).mockImplementation(async (name) => {
switch (name) {
case "cacheFolder":
return ".yarn/cache";
case "deferredVersionFolder":
return ".yarn/versions";
case "installStatePath":
return ".yarn/install-state.gz";
case "patchFolder":
return ".yarn/patches";
case "pnpUnpluggedFolder":
return ".yarn/unplugged";
case "virtualFolder":
return ".yarn/__virtual__";
}
throw new Error(`unknown config: ${name}`);
});

const cachePaths = await getCachePaths();
const expectedCachePaths = [
Expand Down
14 changes: 4 additions & 10 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ describe("install Yarn dependencies", () => {
beforeEach(async () => {
const { getCacheKey, getCachePaths } = await import("./cache.js");

(getCacheKey as jest.Mock<typeof getCacheKey>).mockResolvedValue(cacheKey);
(getCachePaths as jest.Mock<typeof getCachePaths>).mockResolvedValue(
cachePaths,
);
jest.mocked(getCacheKey).mockResolvedValue(cacheKey);
jest.mocked(getCachePaths).mockResolvedValue(cachePaths);
});

it("should install Yarn dependencies and save to cache", async () => {
Expand All @@ -38,9 +36,7 @@ describe("install Yarn dependencies", () => {
const { getCacheKey, getCachePaths } = await import("./cache.js");
const { main } = await import("./main.js");

(restoreCache as jest.Mock<typeof restoreCache>).mockResolvedValue(
undefined,
);
jest.mocked(restoreCache).mockResolvedValue(undefined);

await main();

Expand All @@ -63,9 +59,7 @@ describe("install Yarn dependencies", () => {
const { getCacheKey, getCachePaths } = await import("./cache.js");
const { main } = await import("./main.js");

(restoreCache as jest.Mock<typeof restoreCache>).mockResolvedValue(
cacheKey,
);
jest.mocked(restoreCache).mockResolvedValue(cacheKey);

await main();

Expand Down
4 changes: 2 additions & 2 deletions src/yarn/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ it("should get Yarn config", async () => {
const { getExecOutput } = await import("@actions/exec");
const { getYarnConfig } = await import("./index.js");

(getExecOutput as jest.Mock<typeof getExecOutput>).mockResolvedValueOnce({
jest.mocked(getExecOutput).mockResolvedValueOnce({
exitCode: 0,
stdout: `{"key":"globalFolder","effective":"/.yarn/berry","source":"<default>","description":"Folder where all system-global files are stored","type":"ABSOLUTE_PATH","default":"/.yarn/berry"}\n`,
stderr: "",
Expand All @@ -49,7 +49,7 @@ it("should get Yarn version", async () => {
const { getExecOutput } = await import("@actions/exec");
const { getYarnVersion } = await import("./index.js");

(getExecOutput as jest.Mock<typeof getExecOutput>).mockResolvedValueOnce({
jest.mocked(getExecOutput).mockResolvedValueOnce({
exitCode: 0,
stdout: "1.2.3",
stderr: "",
Expand Down
16 changes: 7 additions & 9 deletions src/yarn/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,18 @@ it("should install package using Yarn", async () => {
const { exec } = await import("@actions/exec");
const { yarnInstall } = await import("./install.js");

(exec as jest.Mock<typeof exec>).mockImplementation(
async (commandLine, args, options) => {
options?.listeners?.stdline(
`{"type":"info","name":null,"displayName":"YN0000","indent":"","data":"└ Completed"}`,
);
return 0;
},
);
jest.mocked(exec).mockImplementation(async (commandLine, args, options) => {
options?.listeners?.stdline(
`{"type":"info","name":null,"displayName":"YN0000","indent":"","data":"└ Completed"}`,
);
return 0;
});

await yarnInstall();

expect(exec).toHaveBeenCalledTimes(1);

const execCall = (exec as jest.Mock<typeof exec>).mock.calls[0];
const execCall = jest.mocked(exec).mock.calls[0];
expect(execCall).toHaveLength(3);
expect(execCall[0]).toBe("corepack");
expect(execCall[1]).toEqual(["yarn", "install", "--json"]);
Expand Down

0 comments on commit b505cef

Please sign in to comment.