Skip to content

Commit

Permalink
test(frolint): Adopt to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
chloe463 committed Dec 14, 2023
1 parent bfa98b3 commit b749500
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions packages/frolint/src/utils/__tests__/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { jest } from "@jest/globals";
import { execSync } from "child_process";
import { getPreCommitHookPath, isInsideGitRepository } from "../git.js";

jest.mock("child_process");
jest.mock("fs");
jest.unstable_mockModule("child_process", () => ({
execSync: (command: string, options: any) => {
if (command.includes("hook")) return Buffer.from(".git/hooks\n");
if (options && options.cwd === "/") {
return Buffer.from("true\n");
}
return Buffer.from("false\n");
},
}));
const { getPreCommitHookPath, isInsideGitRepository } = await import("../git.js");

// mock({
// "/.git": {},
Expand All @@ -18,38 +24,21 @@ describe("git", () => {
});

describe("isInsideGitRepository", () => {
beforeEach(() => {
// eslint-disable-next-line
// @ts-ignore
(execSync as jest.MockedFunction<typeof execSync>).mockImplementation((_command, options) => {
if (options && options.cwd === "/") {
return Buffer.from("true\n");
}
return Buffer.from("false\n");
});
});

it("should return true", () => {
it("should return true", async () => {
const expected = isInsideGitRepository("/");

expect(expected).toBe(true);
});

it("should return false", () => {
it("should return false", async () => {
const expected = isInsideGitRepository("/outside");

expect(expected).toBe(false);
});
});

describe("getPreCommitHookPath", () => {
it("should return pre-commit hook path", () => {
// eslint-disable-next-line
// @ts-ignore
(execSync as jest.MockedFunction<typeof execSync>).mockImplementation((_command, _options) => {
return Buffer.from(".git/hooks\n");
});

it("should return pre-commit hook path", async () => {
expect(getPreCommitHookPath("/").endsWith(".git/hooks/pre-commit")).toBe(true);
expect(getPreCommitHookPath().endsWith(".git/hooks/pre-commit")).toBe(true);
});
Expand Down

0 comments on commit b749500

Please sign in to comment.