-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhistory.unit.test.ts
More file actions
111 lines (95 loc) · 3.22 KB
/
history.unit.test.ts
File metadata and controls
111 lines (95 loc) · 3.22 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { describe, expect, vi } from 'vitest';
import { MINIMAL_PLUGIN_CONFIG_MOCK } from '@code-pushup/test-fixtures';
import { getCurrentBranchOrTag, safeCheckout } from '@code-pushup/utils';
import { collectAndPersistReports } from './collect-and-persist.js';
import { type HistoryOptions, history } from './history.js';
import { upload } from './upload.js';
vi.mock('@code-pushup/utils', async () => {
const utils: object = await vi.importActual('@code-pushup/utils');
return {
...utils,
safeCheckout: vi.fn(),
getCurrentBranchOrTag: vi.fn().mockReturnValue('main'),
};
});
vi.mock('./collect-and-persist', () => ({
collectAndPersistReports: vi.fn(),
}));
vi.mock('./upload', () => ({
upload: vi.fn(),
}));
describe('history', () => {
const historyBaseOptions: HistoryOptions = {
persist: {
outputDir: '.code-pushup',
filename: 'history-report',
format: ['json'],
},
plugins: [MINIMAL_PLUGIN_CONFIG_MOCK],
};
it('should check out all passed commits and reset to initial branch or tag', async () => {
await history(historyBaseOptions, ['abc', 'def']);
expect(getCurrentBranchOrTag).toHaveBeenCalledTimes(1);
expect(safeCheckout).toHaveBeenCalledTimes(3);
// walk commit history
expect(safeCheckout).toHaveBeenNthCalledWith(1, 'abc', undefined);
expect(safeCheckout).toHaveBeenNthCalledWith(2, 'def', undefined);
// reset
expect(safeCheckout).toHaveBeenNthCalledWith(3, 'main', undefined);
});
it('should return correct number of results', async () => {
const results = await history(historyBaseOptions, ['abc', 'def']);
expect(results).toStrictEqual(['abc-report', 'def-report']);
});
it('should call collect with correct filename and format', async () => {
await history(historyBaseOptions, ['abc']);
expect(collectAndPersistReports).toHaveBeenCalledTimes(1);
expect(collectAndPersistReports).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
persist: expect.objectContaining({
filename: 'abc-report',
format: ['json'],
}),
}),
);
});
it('should call upload by default', async () => {
const historyOptions = {
...historyBaseOptions,
upload: {
server: 'https://server.com/api',
project: 'cli',
apiKey: '1234',
organization: 'code-pushup',
timeout: 4000,
},
};
await history(historyOptions, ['abc']);
expect(upload).toHaveBeenCalledTimes(1);
expect(upload).toHaveBeenCalledWith(
expect.objectContaining({
persist: expect.objectContaining({ filename: 'abc-report' }),
}),
);
});
it('should not call upload if skipUploads is set to false', async () => {
const historyOptions = {
...historyBaseOptions,
upload: {
server: 'https://server.com/api',
project: 'cli',
apiKey: '1234',
organization: 'code-pushup',
timeout: 4000,
},
skipUploads: true,
};
await history(historyOptions, ['abc']);
expect(upload).not.toHaveBeenCalled();
});
it('should not call upload if upload config is not given', async () => {
await history(historyBaseOptions, ['abc']);
expect(upload).not.toHaveBeenCalled();
});
});