|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { PersistConfig } from '@code-pushup/models'; |
| 3 | +import * as utilsModule from '@code-pushup/utils'; |
| 4 | +import { createRunnerFunction } from './index.js'; |
| 5 | +import * as lintModule from './lint.js'; |
| 6 | +import * as transformModule from './transform.js'; |
| 7 | +import type { LinterOutput } from './types.js'; |
| 8 | +import * as utilsRunnerModule from './utils.js'; |
| 9 | + |
| 10 | +describe('createRunnerFunction', () => { |
| 11 | + const asyncSequentialSpy = vi.spyOn(utilsModule, 'asyncSequential'); |
| 12 | + const uiLoggerLogSpy = vi.fn(); |
| 13 | + vi.spyOn(utilsModule, 'ui').mockReturnValue({ |
| 14 | + logger: { |
| 15 | + log: uiLoggerLogSpy, |
| 16 | + flushLogs: vi.fn(), |
| 17 | + }, |
| 18 | + switchMode: vi.fn(), |
| 19 | + } as any); |
| 20 | + const lintSpy = vi.spyOn(lintModule, 'lint'); |
| 21 | + const mergeLinterOutputsSpy = vi.spyOn(transformModule, 'mergeLinterOutputs'); |
| 22 | + const lintResultsToAuditsSpy = vi.spyOn( |
| 23 | + transformModule, |
| 24 | + 'lintResultsToAudits', |
| 25 | + ); |
| 26 | + const loadArtifactsSpy = vi.spyOn(utilsRunnerModule, 'loadArtifacts'); |
| 27 | + |
| 28 | + const mockLinterOutputs: LinterOutput[] = [ |
| 29 | + { |
| 30 | + results: [], |
| 31 | + ruleOptionsPerFile: {}, |
| 32 | + }, |
| 33 | + ]; |
| 34 | + |
| 35 | + const mockMergedOutput: LinterOutput = { |
| 36 | + results: [], |
| 37 | + ruleOptionsPerFile: {}, |
| 38 | + }; |
| 39 | + |
| 40 | + const mockFailedAudits = [ |
| 41 | + { |
| 42 | + slug: 'no-unused-vars', |
| 43 | + score: 0, |
| 44 | + value: 1, |
| 45 | + displayValue: '1 error', |
| 46 | + details: { issues: [] }, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + vi.clearAllMocks(); |
| 52 | + asyncSequentialSpy.mockResolvedValue(mockLinterOutputs); |
| 53 | + mergeLinterOutputsSpy.mockReturnValue(mockMergedOutput); |
| 54 | + lintResultsToAuditsSpy.mockReturnValue(mockFailedAudits); |
| 55 | + loadArtifactsSpy.mockResolvedValue(mockLinterOutputs); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should create a runner function and call dependencies', async () => { |
| 59 | + const runnerFunction = await createRunnerFunction({ |
| 60 | + audits: [ |
| 61 | + { |
| 62 | + slug: 'no-unused-vars', |
| 63 | + title: 'No unused vars', |
| 64 | + description: 'Test rule', |
| 65 | + }, |
| 66 | + ], |
| 67 | + targets: [{ eslintrc: '.eslintrc.js', patterns: ['src/**/*.js'] }], |
| 68 | + }); |
| 69 | + const persistConfig: PersistConfig = { outputDir: '/tmp/output' }; |
| 70 | + await expect(runnerFunction(persistConfig)).resolves.not.toThrow(); |
| 71 | + expect(uiLoggerLogSpy).toHaveBeenCalledWith( |
| 72 | + 'ESLint plugin executing 1 lint targets', |
| 73 | + ); |
| 74 | + expect(asyncSequentialSpy).toHaveBeenCalledWith( |
| 75 | + expect.arrayContaining([ |
| 76 | + expect.objectContaining({ |
| 77 | + outputDir: '/tmp/output', |
| 78 | + }), |
| 79 | + ]), |
| 80 | + lintSpy, |
| 81 | + ); |
| 82 | + expect(mergeLinterOutputsSpy).toHaveBeenCalledWith(mockLinterOutputs); |
| 83 | + expect(lintResultsToAuditsSpy).toHaveBeenCalledWith(mockMergedOutput); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should call loadArtifacts when artifacts provided', async () => { |
| 87 | + const runnerFunction = await createRunnerFunction({ |
| 88 | + audits: [ |
| 89 | + { |
| 90 | + slug: 'no-unused-vars', |
| 91 | + title: 'No unused vars', |
| 92 | + description: 'Test rule', |
| 93 | + }, |
| 94 | + ], |
| 95 | + targets: [{ eslintrc: '.eslintrc.js', patterns: ['src/**/*.js'] }], |
| 96 | + artifacts: { |
| 97 | + artifactsPaths: ['/path/to/artifact.json'], |
| 98 | + }, |
| 99 | + }); |
| 100 | + const persistConfig: PersistConfig = { outputDir: '/tmp/output' }; |
| 101 | + await expect(runnerFunction(persistConfig)).resolves.not.toThrow(); |
| 102 | + |
| 103 | + expect(loadArtifactsSpy).toHaveBeenCalledWith({ |
| 104 | + artifactsPaths: ['/path/to/artifact.json'], |
| 105 | + }); |
| 106 | + expect(asyncSequentialSpy).not.toHaveBeenCalled(); |
| 107 | + expect(mergeLinterOutputsSpy).toHaveBeenCalledWith(mockLinterOutputs); |
| 108 | + expect(lintResultsToAuditsSpy).toHaveBeenCalledWith(mockMergedOutput); |
| 109 | + }); |
| 110 | +}); |
0 commit comments