|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { type AuditOutput, DEFAULT_PERSIST_CONFIG } from '@code-pushup/models'; |
| 3 | +import * as runAxe from './run-axe.js'; |
| 4 | +import { createRunnerFunction } from './runner.js'; |
| 5 | + |
| 6 | +vi.mock('./run-axe.js', () => ({ |
| 7 | + runAxeForUrl: vi.fn(), |
| 8 | + closeBrowser: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +describe('createRunnerFunction', () => { |
| 12 | + const mockRunAxeForUrl = vi.mocked(runAxe.runAxeForUrl); |
| 13 | + const mockCloseBrowser = vi.mocked(runAxe.closeBrowser); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + const createMockAuditOutput = (slug: string, score = 1): AuditOutput => ({ |
| 20 | + slug, |
| 21 | + score, |
| 22 | + value: 0, |
| 23 | + displayValue: 'No violations found', |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle single URL without adding index to audit slugs', async () => { |
| 27 | + const mockResults = [ |
| 28 | + createMockAuditOutput('image-alt'), |
| 29 | + createMockAuditOutput('html-has-lang'), |
| 30 | + ]; |
| 31 | + mockRunAxeForUrl.mockResolvedValue(mockResults); |
| 32 | + |
| 33 | + const runnerFn = createRunnerFunction(['https://example.com'], []); |
| 34 | + const results = await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 35 | + |
| 36 | + expect(mockRunAxeForUrl).toHaveBeenCalledWith('https://example.com', []); |
| 37 | + expect(mockCloseBrowser).toHaveBeenCalled(); |
| 38 | + expect(results).toEqual(mockResults); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should handle multiple URLs and add index to audit slugs', async () => { |
| 42 | + const mockResults1 = [ |
| 43 | + createMockAuditOutput('image-alt'), |
| 44 | + createMockAuditOutput('html-has-lang'), |
| 45 | + ]; |
| 46 | + const mockResults2 = [ |
| 47 | + createMockAuditOutput('image-alt'), |
| 48 | + createMockAuditOutput('color-contrast'), |
| 49 | + ]; |
| 50 | + |
| 51 | + mockRunAxeForUrl |
| 52 | + .mockResolvedValueOnce(mockResults1) |
| 53 | + .mockResolvedValueOnce(mockResults2); |
| 54 | + |
| 55 | + const runnerFn = createRunnerFunction( |
| 56 | + ['https://example.com', 'https://another-example.org'], |
| 57 | + [], |
| 58 | + ); |
| 59 | + const results = await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 60 | + |
| 61 | + expect(mockRunAxeForUrl).toHaveBeenCalledTimes(2); |
| 62 | + expect(mockRunAxeForUrl).toHaveBeenNthCalledWith( |
| 63 | + 1, |
| 64 | + 'https://example.com', |
| 65 | + [], |
| 66 | + ); |
| 67 | + expect(mockRunAxeForUrl).toHaveBeenNthCalledWith( |
| 68 | + 2, |
| 69 | + 'https://another-example.org', |
| 70 | + [], |
| 71 | + ); |
| 72 | + expect(mockCloseBrowser).toHaveBeenCalled(); |
| 73 | + |
| 74 | + expect(results).toHaveLength(4); |
| 75 | + expect(results.map(({ slug }) => slug)).toEqual([ |
| 76 | + 'image-alt-1', |
| 77 | + 'html-has-lang-1', |
| 78 | + 'image-alt-2', |
| 79 | + 'color-contrast-2', |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should pass ruleIds to runAxeForUrl', async () => { |
| 84 | + const mockResults = [ |
| 85 | + createMockAuditOutput('image-alt'), |
| 86 | + createMockAuditOutput('html-has-lang'), |
| 87 | + ]; |
| 88 | + mockRunAxeForUrl.mockResolvedValue(mockResults); |
| 89 | + |
| 90 | + const ruleIds = ['image-alt', 'html-has-lang']; |
| 91 | + const runnerFn = createRunnerFunction(['https://example.com'], ruleIds); |
| 92 | + await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 93 | + |
| 94 | + expect(mockRunAxeForUrl).toHaveBeenCalledWith( |
| 95 | + 'https://example.com', |
| 96 | + ruleIds, |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should continue with other URLs when one fails in multiple URL scenario', async () => { |
| 101 | + const mockResults = [createMockAuditOutput('image-alt')]; |
| 102 | + |
| 103 | + mockRunAxeForUrl |
| 104 | + .mockRejectedValueOnce(new Error('Failed to load page')) |
| 105 | + .mockResolvedValueOnce(mockResults); |
| 106 | + |
| 107 | + const runnerFn = createRunnerFunction( |
| 108 | + ['https://broken.com', 'https://working.com'], |
| 109 | + [], |
| 110 | + ); |
| 111 | + const results = await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 112 | + |
| 113 | + expect(mockRunAxeForUrl).toHaveBeenCalledTimes(2); |
| 114 | + expect(mockCloseBrowser).toHaveBeenCalled(); |
| 115 | + expect(results).toHaveLength(1); |
| 116 | + expect(results[0]?.slug).toBe('image-alt-2'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should throw error if all URLs fail in multiple URL scenario', async () => { |
| 120 | + mockRunAxeForUrl.mockRejectedValue(new Error('Failed to load page')); |
| 121 | + |
| 122 | + const runnerFn = createRunnerFunction( |
| 123 | + ['https://example.com', 'https://another-example.com'], |
| 124 | + [], |
| 125 | + ); |
| 126 | + |
| 127 | + await expect(runnerFn({ persist: DEFAULT_PERSIST_CONFIG })).rejects.toThrow( |
| 128 | + 'Axe failed to produce results for all URLs.', |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should throw error when single URL fails', async () => { |
| 133 | + mockRunAxeForUrl.mockRejectedValue(new Error('Failed to load page')); |
| 134 | + |
| 135 | + const runnerFn = createRunnerFunction(['https://example.com'], []); |
| 136 | + |
| 137 | + await expect(runnerFn({ persist: DEFAULT_PERSIST_CONFIG })).rejects.toThrow( |
| 138 | + 'Axe did not produce any results.', |
| 139 | + ); |
| 140 | + }); |
| 141 | +}); |
0 commit comments