-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-user.test.ts
More file actions
43 lines (36 loc) · 1.25 KB
/
git-user.test.ts
File metadata and controls
43 lines (36 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { execSync } from "child_process";
import { getLocalGitUser } from ".";
jest.mock("child_process", () => ({
execSync: jest.fn(),
}));
jest.mock("@shared/messages", () => ({
getRandomMessage: jest.fn(() => "Error message"),
MessageType: {
NoGitConfig: "NoGitConfig",
},
}));
describe("getLocalGitUser", () => {
afterEach(() => {
jest.clearAllMocks();
});
it("returns an user with an username", () => {
(execSync as jest.Mock).mockReturnValueOnce("tomhagen");
expect(getLocalGitUser()).toEqual({ name: "tomhagen" });
});
it("returns an user with a trimmed username", () => {
(execSync as jest.Mock).mockReturnValueOnce("tomhagen ");
expect(getLocalGitUser()).toEqual({ name: "tomhagen" });
(execSync as jest.Mock).mockReturnValueOnce("tomhagen\n\n");
expect(getLocalGitUser()).toEqual({ name: "tomhagen" });
});
it("throws an error if execSync fails", () => {
(execSync as jest.Mock).mockImplementationOnce(() => {
throw new Error();
});
expect(() => getLocalGitUser()).toThrow("Error message");
});
it("throws an error if there is no username in the git config", () => {
(execSync as jest.Mock).mockReturnValueOnce(undefined);
expect(() => getLocalGitUser()).toThrow();
});
});