Skip to content

Commit 827b7e9

Browse files
committed
test: rewrite git tests
1 parent e3b94df commit 827b7e9

File tree

1 file changed

+159
-132
lines changed

1 file changed

+159
-132
lines changed

test/git.spec.ts

Lines changed: 159 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,179 @@
1-
import type { JestMockCompat } from 'vitest';
2-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { afterEach, describe, expect, it } from 'vitest';
2+
import validator from 'validator';
33
import { faker } from '../dist/cjs';
44

5-
describe('git.js', () => {
6-
describe('branch()', () => {
7-
let spy_hacker_noun: JestMockCompat<[], string>;
8-
let spy_hacker_verb: JestMockCompat<[], string>;
9-
10-
beforeEach(() => {
11-
spy_hacker_noun = vi.spyOn(faker.hacker, 'noun');
12-
spy_hacker_verb = vi.spyOn(faker.hacker, 'verb');
13-
});
14-
15-
afterEach(() => {
16-
spy_hacker_noun.mockRestore();
17-
spy_hacker_verb.mockRestore();
18-
});
19-
20-
it('returns a branch with hacker noun and verb', () => {
21-
faker.git.branch();
22-
23-
expect(spy_hacker_noun).toHaveBeenCalledOnce();
24-
expect(spy_hacker_verb).toHaveBeenCalledOnce();
25-
});
5+
const seededRuns = [
6+
{
7+
seed: 42,
8+
expectations: {
9+
branch: {
10+
noArgs: 'array-transmit',
11+
},
12+
commitEntry: {
13+
noArgs: '',
14+
},
15+
commitMessage: {
16+
noArgs: 'navigate neural capacitor',
17+
},
18+
commitSha: {
19+
noArgs: '5cf2bc99272107d592ba00fbdf302f2949806048',
20+
},
21+
shortSha: {
22+
noArgs: '5cf2bc9',
23+
},
24+
},
25+
},
26+
{
27+
seed: 1337,
28+
expectations: {
29+
branch: {
30+
noArgs: 'port-quantify',
31+
},
32+
commitEntry: {
33+
noArgs: '',
34+
},
35+
commitMessage: {
36+
noArgs: 'compress multi-byte panel',
37+
},
38+
commitSha: {
39+
noArgs: '48234870538945f4b41c61a52bf27dccc0576698',
40+
},
41+
shortSha: {
42+
noArgs: '4823487',
43+
},
44+
},
45+
},
46+
{
47+
seed: 1211,
48+
expectations: {
49+
branch: {
50+
noArgs: 'capacitor-connect',
51+
},
52+
commitEntry: {
53+
noArgs: '',
54+
},
55+
commitMessage: {
56+
noArgs: 'reboot online circuit',
57+
},
58+
commitSha: {
59+
noArgs: 'e7ec32f0a2a3c652bbd0caabde64dfdf379e3259',
60+
},
61+
shortSha: {
62+
noArgs: 'e7ec32f',
63+
},
64+
},
65+
},
66+
];
67+
68+
const NON_SEEDED_BASED_RUN = 5;
69+
70+
const functionNames = [
71+
'branch',
72+
'commitEntry',
73+
'commitMessage',
74+
'commitSha',
75+
'shortSha',
76+
];
77+
78+
describe('git', () => {
79+
afterEach(() => {
80+
faker.locale = 'en';
2681
});
2782

28-
describe('commitEntry()', () => {
29-
let spy_git_commitMessage: JestMockCompat<[], string>;
30-
let spy_git_commitSha: JestMockCompat<[], string>;
31-
let spy_internet_email: JestMockCompat<
32-
[firstName?: string, lastName?: string, provider?: string],
33-
string
34-
>;
35-
let spy_name_firstName: JestMockCompat<[gender?: string | number], string>;
36-
let spy_name_lastName: JestMockCompat<[gender?: string | number], string>;
37-
let spy_datatype_number: JestMockCompat<
38-
[
39-
options?:
40-
| number
41-
| {
42-
min?: number;
43-
max?: number;
44-
precision?: number;
45-
}
46-
],
47-
number
48-
>;
49-
50-
beforeEach(() => {
51-
spy_git_commitMessage = vi.spyOn(faker.git, 'commitMessage');
52-
spy_git_commitSha = vi.spyOn(faker.git, 'commitSha');
53-
spy_internet_email = vi.spyOn(faker.internet, 'email');
54-
spy_name_firstName = vi.spyOn(faker.name, 'firstName');
55-
spy_name_lastName = vi.spyOn(faker.name, 'lastName');
56-
spy_datatype_number = vi.spyOn(faker.datatype, 'number');
57-
});
58-
59-
afterEach(() => {
60-
spy_git_commitMessage.mockRestore();
61-
spy_git_commitSha.mockRestore();
62-
spy_internet_email.mockRestore();
63-
spy_name_firstName.mockRestore();
64-
spy_name_lastName.mockRestore();
65-
spy_datatype_number.mockRestore();
83+
for (const { seed, expectations } of seededRuns) {
84+
describe(`seed: ${seed}`, () => {
85+
for (const functionName of functionNames) {
86+
if (functionName === 'commitEntry') {
87+
it.todo(`${functionName}()`);
88+
continue;
89+
}
90+
91+
it(`${functionName}()`, () => {
92+
faker.seed(seed);
93+
94+
const actual = faker.git[functionName]();
95+
expect(actual).toEqual(expectations[functionName].noArgs);
96+
});
97+
}
6698
});
99+
}
67100

68-
it('returns merge entry at random', () => {
69-
faker.git.commitEntry();
101+
// Create and log-back the seed for debug purposes
102+
faker.seed(Math.ceil(Math.random() * 1_000_000_000));
70103

71-
expect(spy_datatype_number).toHaveBeenCalled();
72-
});
73-
74-
it('returns a commit entry with git commit message and sha', () => {
75-
faker.git.commitEntry();
76-
77-
expect(spy_git_commitMessage).toHaveBeenCalledOnce();
78-
expect(spy_git_commitSha).toHaveBeenCalledOnce();
79-
});
80-
81-
it('returns a commit entry with internet email', () => {
82-
faker.git.commitEntry();
104+
describe(`random seeded tests for seed ${faker.seedValue}`, () => {
105+
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
106+
describe('branch()', () => {
107+
it('should return a random branch', () => {
108+
const branch = faker.git.branch();
83109

84-
expect(spy_internet_email).toHaveBeenCalledOnce();
85-
});
86-
87-
it('returns a commit entry with name first and last', () => {
88-
faker.git.commitEntry();
89-
90-
expect(spy_name_firstName).toHaveBeenCalledTimes(2);
91-
expect(spy_name_lastName).toHaveBeenCalledTimes(2);
92-
});
93-
94-
describe("with options['merge'] equal to true", () => {
95-
let spy_git_shortSha: JestMockCompat<[], string>;
96-
97-
beforeEach(() => {
98-
spy_git_shortSha = vi.spyOn(faker.git, 'shortSha');
110+
expect(branch).toBeTruthy();
111+
expect(typeof branch).toBe('string');
112+
expect(branch).satisfy(validator.isSlug);
113+
});
99114
});
100115

101-
afterEach(() => {
102-
spy_git_shortSha.mockRestore();
116+
describe('commitEntry', () => {
117+
it('should return a random commitEntry', () => {
118+
const commitEntry = faker.git.commitEntry();
119+
120+
expect(commitEntry).toBeTruthy();
121+
expect(typeof commitEntry).toBe('string');
122+
123+
const parts = commitEntry.split(/\r?\n/);
124+
125+
expect(parts.length).greaterThanOrEqual(6);
126+
expect(parts.length).lessThanOrEqual(7);
127+
128+
expect(parts[0]).match(/^commit [a-f0-9]+$/);
129+
if (parts.length === 7) {
130+
expect(parts[1]).match(/^Merge: [a-f0-9]+ [a-f0-9]+$/);
131+
expect(parts[2]).match(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
132+
expect(parts[3]).match(/^Date: .+$/);
133+
expect(parts[4]).toBe('');
134+
expect(parts[5]).match(/^\s{4}.+$/);
135+
} else {
136+
expect(parts[1]).match(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
137+
expect(parts[2]).match(/^Date: .+$/);
138+
expect(parts[3]).toBe('');
139+
expect(parts[4]).match(/^\s{4}.+$/);
140+
}
141+
});
103142
});
104143

105-
it('returns a commit entry with merge details', () => {
106-
faker.git.commitEntry({ merge: true });
144+
describe('commitMessage', () => {
145+
it('should return a random commitMessage', () => {
146+
const commitMessage = faker.git.commitMessage();
107147

108-
expect(spy_git_shortSha).toHaveBeenCalledTimes(2);
109-
});
110-
});
111-
});
148+
expect(commitMessage).toBeTruthy();
149+
expect(typeof commitMessage).toBe('string');
112150

113-
describe('commitMessage()', () => {
114-
let spy_hacker_verb: JestMockCompat<[], string>;
115-
let spy_hacker_adjective: JestMockCompat<[], string>;
116-
let spy_hacker_noun: JestMockCompat<[], string>;
117-
118-
beforeEach(() => {
119-
spy_hacker_verb = vi.spyOn(faker.hacker, 'verb');
120-
spy_hacker_adjective = vi.spyOn(faker.hacker, 'adjective');
121-
spy_hacker_noun = vi.spyOn(faker.hacker, 'noun');
122-
});
123-
124-
afterEach(() => {
125-
spy_hacker_verb.mockRestore();
126-
spy_hacker_adjective.mockRestore();
127-
spy_hacker_noun.mockRestore();
128-
});
151+
const parts = commitMessage.split(' ');
152+
expect(parts.length).greaterThanOrEqual(3);
153+
});
154+
});
129155

130-
it('returns a commit message with hacker noun, adj and verb', () => {
131-
faker.git.commitMessage();
156+
describe('commitSha', () => {
157+
it('should return a random commitSha', () => {
158+
const commitSha = faker.git.commitSha();
132159

133-
expect(spy_hacker_verb).toHaveBeenCalledOnce();
134-
expect(spy_hacker_adjective).toHaveBeenCalledOnce();
135-
expect(spy_hacker_noun).toHaveBeenCalledOnce();
136-
});
137-
});
160+
expect(commitSha).toBeTruthy();
161+
expect(typeof commitSha).toBe('string');
162+
expect(commitSha).satisfy(validator.isHexadecimal);
163+
expect(commitSha).toHaveLength(40);
164+
});
165+
});
138166

139-
describe('commitSha()', () => {
140-
it('returns a random commit SHA', () => {
141-
const commitSha = faker.git.commitSha();
142-
expect(commitSha).match(/^[a-f0-9]{40}$/);
143-
});
144-
});
167+
describe('shortSha', () => {
168+
it('should return a random shortSha', () => {
169+
const shortSha = faker.git.shortSha();
145170

146-
describe('shortSha()', () => {
147-
it('returns a random short SHA', () => {
148-
const shortSha = faker.git.shortSha();
149-
expect(shortSha).match(/^[a-f0-9]{7}$/);
150-
});
171+
expect(shortSha).toBeTruthy();
172+
expect(typeof shortSha).toBe('string');
173+
expect(shortSha).satisfy(validator.isHexadecimal);
174+
expect(shortSha).toHaveLength(7);
175+
});
176+
});
177+
}
151178
});
152179
});

0 commit comments

Comments
 (0)