|
| 1 | +import path from 'node:path'; |
| 2 | +import { pathToFileURL } from 'node:url'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { DEFAULT_PERSIST_CONFIG } from '@code-pushup/models'; |
| 5 | +import { createRunnerFunction } from './runner.js'; |
| 6 | + |
| 7 | +describe('createRunnerFunction', () => { |
| 8 | + const fixturesDir = path.join( |
| 9 | + process.cwd(), |
| 10 | + 'packages/plugin-axe/mocks/fixtures', |
| 11 | + ); |
| 12 | + |
| 13 | + const getFixtureUrl = (filename: string): string => { |
| 14 | + return pathToFileURL(path.join(fixturesDir, filename)).href; |
| 15 | + }; |
| 16 | + |
| 17 | + it('should detect violations on page with accessibility issues', async () => { |
| 18 | + const urls = [getFixtureUrl('violations.html')]; |
| 19 | + const runnerFn = createRunnerFunction(urls, []); |
| 20 | + const results = await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 21 | + |
| 22 | + expect(results.length).toBeGreaterThan(0); |
| 23 | + expect(results.filter(({ score }) => score === 0).length).toBeGreaterThan( |
| 24 | + 0, |
| 25 | + ); |
| 26 | + const imageAltAudit = results.find(({ slug }) => slug === 'image-alt'); |
| 27 | + expect(imageAltAudit?.score).toBe(0); |
| 28 | + expect(imageAltAudit?.value).toBeGreaterThan(0); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should handle multiple URLs and add index to slugs', async () => { |
| 32 | + const urls = [ |
| 33 | + getFixtureUrl('accessible.html'), |
| 34 | + getFixtureUrl('minimal.html'), |
| 35 | + ]; |
| 36 | + const runnerFn = createRunnerFunction(urls, []); |
| 37 | + const results = await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 38 | + |
| 39 | + expect(results.length).toBeGreaterThan(0); |
| 40 | + results.forEach(({ slug }) => expect(slug).toMatch(/-[12]$/)); |
| 41 | + |
| 42 | + const url1Audits = results.filter(({ slug }) => slug.endsWith('-1')); |
| 43 | + const url2Audits = results.filter(({ slug }) => slug.endsWith('-2')); |
| 44 | + |
| 45 | + expect(url1Audits.length).toBeGreaterThan(0); |
| 46 | + expect(url2Audits.length).toBeGreaterThan(0); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should filter audits based on ruleIds', async () => { |
| 50 | + const urls = [getFixtureUrl('accessible.html')]; |
| 51 | + const ruleIds = ['image-alt', 'html-has-lang']; |
| 52 | + const runnerFn = createRunnerFunction(urls, ruleIds); |
| 53 | + const results = await runnerFn({ persist: DEFAULT_PERSIST_CONFIG }); |
| 54 | + |
| 55 | + expect(results.length).toBeLessThanOrEqual(ruleIds.length); |
| 56 | + results.forEach(({ slug }) => expect(ruleIds).toContain(slug)); |
| 57 | + }); |
| 58 | +}, 60_000); |
0 commit comments