|
| 1 | +import { generateStdoutReport } from './utils'; |
| 2 | +import { createReducedReport } from '../report/utils'; |
| 3 | +import { generateMdReport } from '../../../assert/utils/md-report'; |
| 4 | +import { persistFlow } from './persist-flow'; |
| 5 | +import { writeFile } from '../../../../core/file'; |
| 6 | +import { log } from '../../../../core/loggin'; |
| 7 | + |
| 8 | +import type { ReducedReport } from '../report/types'; |
| 9 | +import type { UserFlow } from '../../../../hacky-things/lighthouse'; |
| 10 | + |
| 11 | +jest.mock('node:fs', () => ({ |
| 12 | + existsSync: jest.fn().mockReturnValue(true) |
| 13 | +})); |
| 14 | +jest.mock('../../../../hacky-things/lighthouse') |
| 15 | +jest.mock('../../../../core/file'); |
| 16 | +jest.mock('../../../../core/loggin'); |
| 17 | +jest.mock('./utils'); |
| 18 | +jest.mock('../../../assert/utils/md-report', () => ({ |
| 19 | + generateMdReport: jest.fn().mockReturnValue('Mock Md Report') |
| 20 | +})); |
| 21 | +jest.mock('../report/utils', () => ({ |
| 22 | + createReducedReport: jest.fn(), |
| 23 | + toReportName: jest.fn().mockReturnValue('report'), |
| 24 | +})); |
| 25 | + |
| 26 | +const flow = { |
| 27 | + name: 'flow-name', |
| 28 | + createFlowResult: jest.fn(), |
| 29 | + generateReport: jest.fn() |
| 30 | +} satisfies UserFlow; |
| 31 | + |
| 32 | +describe('persist flow reports in specified format', () => { |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not save any reports if no format is given', async () => { |
| 39 | + await persistFlow(flow, { outPath: '', format: [], url: 'mock.com' }); |
| 40 | + expect(writeFile).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not save any report if format is only stdout', async () => { |
| 44 | + await persistFlow(flow, { outPath: '', format: ['stdout'], url: 'mock.com' }); |
| 45 | + expect(writeFile).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return an empty list if format is only stdout', async () => { |
| 49 | + const reports = await persistFlow(flow, { outPath: '', format: ['stdout'], url: 'mock.com' }); |
| 50 | + expect(reports).toStrictEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should log a report if stdout is passed as format', async () => { |
| 54 | + const generateStdoutReportSpy = jest.mocked(generateStdoutReport).mockReturnValue('Mock stdout report') |
| 55 | + await persistFlow(flow, { outPath: '', format: ['stdout'], url: 'mock.com' }); |
| 56 | + expect(generateStdoutReportSpy).toHaveBeenCalled(); |
| 57 | + expect(log).toHaveBeenCalledWith('Mock stdout report'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should extract a json report from UserFlow if json is given as format', async () => { |
| 61 | + const createFlowResultSpy = jest.spyOn(flow, 'createFlowResult'); |
| 62 | + await persistFlow(flow, { outPath: '', format: ['json'], url: 'mock.com' }); |
| 63 | + expect(createFlowResultSpy).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should save the report in json if json is given as format', async () => { |
| 67 | + jest.spyOn(flow, 'createFlowResult').mockResolvedValue({mock: 'jsonResult'}); |
| 68 | + await persistFlow(flow, { outPath: '', format: ['json'], url: 'mock.com' }); |
| 69 | + expect(writeFile).toHaveBeenCalledWith('report.json', JSON.stringify({mock: 'jsonResult'})); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return the path to the json report if json is given as format', async () => { |
| 73 | + const reports = await persistFlow(flow, { outPath: '', format: ['json'], url: 'mock.com' }); |
| 74 | + expect(reports).toStrictEqual(['report.json']); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should extract an html report from UserFlow if html is given as format', async () => { |
| 78 | + const generateReportSpy = jest.spyOn(flow, 'generateReport'); |
| 79 | + await persistFlow(flow, { outPath: '', format: ['html'], url: 'mock.com' }); |
| 80 | + expect(generateReportSpy).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should save the report in html if html is given as format', async () => { |
| 84 | + jest.spyOn(flow, 'generateReport').mockResolvedValue('Mock HTML Report'); |
| 85 | + await persistFlow(flow, { outPath: '', format: ['html'], url: 'mock.com' }); |
| 86 | + expect(writeFile).toHaveBeenCalledWith('report.html', 'Mock HTML Report'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return the path to the html report if html is given as format', async () => { |
| 90 | + const reports = await persistFlow(flow, { outPath: '', format: ['html'], url: 'mock.com' }); |
| 91 | + expect(reports).toStrictEqual(['report.html']); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should extract an md report from the json report if md is given as format', async () => { |
| 95 | + jest.spyOn(flow, 'createFlowResult').mockResolvedValue({mock: 'base for md report'}); |
| 96 | + const createReducedReportSpy = jest.mocked(createReducedReport).mockReturnValue({mock: 'reduced report'} as any as ReducedReport); |
| 97 | + const generateMdReportMock = jest.mocked(generateMdReport); |
| 98 | + await persistFlow(flow, { outPath: '', format: ['md'], url: 'mock.com' }); |
| 99 | + expect(createReducedReportSpy).toHaveBeenCalledWith({mock: 'base for md report'}) |
| 100 | + expect(generateMdReportMock).toHaveBeenCalledWith({mock: 'reduced report'}); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should save the report in md if md is given as format', async () => { |
| 104 | + await persistFlow(flow, { outPath: '', format: ['md'], url: 'mock.com' }); |
| 105 | + expect(writeFile).toHaveBeenCalledWith('report.md', 'Mock Md Report'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return the path to the markdown report if md is given as format', async () => { |
| 109 | + const reports = await persistFlow(flow, { outPath: '', format: ['md'], url: 'mock.com' }); |
| 110 | + expect(reports).toStrictEqual(['report.md']); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should save the report in multiple formats if multiple formats are given', async () => { |
| 114 | + const reports = await persistFlow(flow, { outPath: '', format: ['html', 'json', 'md', 'stdout'], url: 'mock.com' }); |
| 115 | + expect(reports).toStrictEqual(['report.json', 'report.md', 'report.html']); |
| 116 | + }); |
| 117 | +}); |
0 commit comments