From 2b1cb08655006b0dea6d4613d98f875b0499b99c Mon Sep 17 00:00:00 2001 From: dimaslz Date: Sun, 28 Jan 2024 17:32:15 +0100 Subject: [PATCH] test(cli): update tests --- packages/cli/src/utils/hosts.test.ts | 41 +++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/utils/hosts.test.ts b/packages/cli/src/utils/hosts.test.ts index e5a6ce2..24d6dd4 100644 --- a/packages/cli/src/utils/hosts.test.ts +++ b/packages/cli/src/utils/hosts.test.ts @@ -2,8 +2,13 @@ import consola from "consola"; import fs from "fs"; import { HOSTS_END, HOSTS_START } from "@/constants"; +import { + getContentFromHosts, + setContentToHosts, + updateHosts, +} from "@/utils/hosts"; -import { getContentFromHosts, setContentToHosts, updateHosts } from "./hosts"; +vi.mock("@/utils/update-system-hosts"); describe("Utils - getContentFromHosts, setContentToHosts, updateHosts", () => { test("move domain if already exists without Local SSL config slot", async () => { @@ -14,6 +19,15 @@ describe("Utils - getContentFromHosts, setContentToHosts, updateHosts", () => { const result = await getContentFromHosts(); + // read files + expect(fs.readFileSync).toBeCalledTimes(1); + expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", { + encoding: "utf8", + }); + + // write files + expect(fs.writeFileSync).not.toBeCalled(); + expect(result).toBe(""); }); @@ -25,6 +39,15 @@ describe("Utils - getContentFromHosts, setContentToHosts, updateHosts", () => { const result = await getContentFromHosts(); + // read files + expect(fs.readFileSync).toBeCalledTimes(1); + expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", { + encoding: "utf8", + }); + + // write files + expect(fs.writeFileSync).not.toBeCalled(); + expect(result).toBe(""); }); @@ -42,6 +65,15 @@ ${HOSTS_END} const result = await getContentFromHosts(); + // read files + expect(fs.readFileSync).toBeCalledTimes(1); + expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", { + encoding: "utf8", + }); + + // write files + expect(fs.writeFileSync).not.toBeCalled(); + expect(result).toBe( ` 127.0.0.1 local.domain.com @@ -106,6 +138,13 @@ ${HOSTS_END} `.trim(), ); + // read files + expect(fs.readFileSync).toBeCalledTimes(1); + expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", { + encoding: "utf8", + }); + + // write files expect(fs.writeFileSync).toBeCalledTimes(1); expect(fs.writeFileSync).toBeCalledWith( expect.stringMatching(/\/root\/path\/.tmp-hosts/i),