|
| 1 | +import { describe, expect, vi } from 'vitest'; |
| 2 | +import { MINIMAL_HISTORY_CONFIG_MOCK } from '@code-pushup/test-utils'; |
| 3 | +import { getCurrentBranchOrTag, safeCheckout } from '@code-pushup/utils'; |
| 4 | +import { collectAndPersistReports } from './collect-and-persist'; |
| 5 | +import { HistoryOptions, history, prepareHashes } from './history'; |
| 6 | +import { upload } from './upload'; |
| 7 | + |
| 8 | +vi.mock('@code-pushup/utils', async () => { |
| 9 | + const utils: object = await vi.importActual('@code-pushup/utils'); |
| 10 | + return { |
| 11 | + ...utils, |
| 12 | + safeCheckout: vi.fn(), |
| 13 | + getCurrentBranchOrTag: vi.fn().mockReturnValue('main'), |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock('./collect-and-persist', () => ({ |
| 18 | + collectAndPersistReports: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('./upload', () => ({ |
| 22 | + upload: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +describe('history', () => { |
| 26 | + it('should check out all passed commits and reset to initial branch or tag', async () => { |
| 27 | + await history(MINIMAL_HISTORY_CONFIG_MOCK, ['abc', 'def']); |
| 28 | + |
| 29 | + expect(getCurrentBranchOrTag).toHaveBeenCalledTimes(1); |
| 30 | + |
| 31 | + expect(safeCheckout).toHaveBeenCalledTimes(3); |
| 32 | + // walk commit history |
| 33 | + expect(safeCheckout).toHaveBeenNthCalledWith(1, 'abc', undefined); |
| 34 | + expect(safeCheckout).toHaveBeenNthCalledWith(2, 'def', undefined); |
| 35 | + // reset |
| 36 | + expect(safeCheckout).toHaveBeenNthCalledWith(3, 'main', undefined); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return correct number of results', async () => { |
| 40 | + const historyOptions: HistoryOptions = MINIMAL_HISTORY_CONFIG_MOCK; |
| 41 | + |
| 42 | + const results = await history(historyOptions, ['abc', 'def']); |
| 43 | + |
| 44 | + expect(results).toStrictEqual(['abc-report', 'def-report']); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should call collect with correct filename and format', async () => { |
| 48 | + const historyOptions: HistoryOptions = MINIMAL_HISTORY_CONFIG_MOCK; |
| 49 | + |
| 50 | + await history(historyOptions, ['abc']); |
| 51 | + expect(collectAndPersistReports).toHaveBeenCalledTimes(1); |
| 52 | + expect(collectAndPersistReports).toHaveBeenNthCalledWith( |
| 53 | + 1, |
| 54 | + expect.objectContaining({ |
| 55 | + persist: expect.objectContaining({ |
| 56 | + filename: 'abc-report', |
| 57 | + format: ['json'], |
| 58 | + }), |
| 59 | + }), |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should call upload by default', async () => { |
| 64 | + const historyOptions: HistoryOptions = { |
| 65 | + ...MINIMAL_HISTORY_CONFIG_MOCK, |
| 66 | + upload: { |
| 67 | + server: 'https://server.com/api', |
| 68 | + project: 'cli', |
| 69 | + apiKey: '1234', |
| 70 | + organization: 'code-pushup', |
| 71 | + timeout: 4000, |
| 72 | + }, |
| 73 | + }; |
| 74 | + await history(historyOptions, ['abc']); |
| 75 | + |
| 76 | + expect(upload).toHaveBeenCalledTimes(1); |
| 77 | + expect(upload).toHaveBeenCalledWith( |
| 78 | + expect.objectContaining({ |
| 79 | + persist: expect.objectContaining({ filename: 'abc-report' }), |
| 80 | + }), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not call upload if skipUploads is set to false', async () => { |
| 85 | + const historyOptions: HistoryOptions = { |
| 86 | + ...MINIMAL_HISTORY_CONFIG_MOCK, |
| 87 | + upload: { |
| 88 | + server: 'https://server.com/api', |
| 89 | + project: 'cli', |
| 90 | + apiKey: '1234', |
| 91 | + organization: 'code-pushup', |
| 92 | + timeout: 4000, |
| 93 | + }, |
| 94 | + skipUploads: true, |
| 95 | + }; |
| 96 | + await history(historyOptions, ['abc']); |
| 97 | + |
| 98 | + expect(upload).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should not call upload if upload config is not given', async () => { |
| 102 | + await history(MINIMAL_HISTORY_CONFIG_MOCK, ['abc']); |
| 103 | + |
| 104 | + expect(upload).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('prepareHashes', () => { |
| 109 | + it('should return commit hashes in reverse order', () => { |
| 110 | + expect( |
| 111 | + prepareHashes({ |
| 112 | + all: [ |
| 113 | + { |
| 114 | + hash: '22287eb716a84f82b5d59e7238ffcae7147f707a', |
| 115 | + date: 'Thu Mar 7 20:13:33 2024 +0100', |
| 116 | + message: |
| 117 | + 'test: change test reported to basic in order to work on Windows', |
| 118 | + refs: 'string', |
| 119 | + body: '', |
| 120 | + author_name: 'John Doe', |
| 121 | + author_email: '[email protected]', |
| 122 | + }, |
| 123 | + { |
| 124 | + hash: '111b284e48ddf464a498dcf22426a9ce65e2c01c', |
| 125 | + date: 'Thu Mar 7 20:13:34 2024 +0100', |
| 126 | + message: 'chore: exclude fixtures from ESLint', |
| 127 | + refs: 'string', |
| 128 | + body: '', |
| 129 | + author_name: 'Jane Doe', |
| 130 | + author_email: '[email protected]', |
| 131 | + }, |
| 132 | + ], |
| 133 | + total: 2, |
| 134 | + latest: { |
| 135 | + hash: '22287eb716a84f82b5d59e7238ffcae7147f707a', |
| 136 | + date: 'Thu Mar 7 20:13:33 2024 +0100', |
| 137 | + message: |
| 138 | + 'test: change test reported to basic in order to work on Windows', |
| 139 | + refs: 'string', |
| 140 | + body: '', |
| 141 | + author_name: 'John Doe', |
| 142 | + author_email: '[email protected]', |
| 143 | + }, |
| 144 | + }), |
| 145 | + ).toStrictEqual([ |
| 146 | + '111b284e48ddf464a498dcf22426a9ce65e2c01c', |
| 147 | + '22287eb716a84f82b5d59e7238ffcae7147f707a', |
| 148 | + ]); |
| 149 | + }); |
| 150 | +}); |
0 commit comments