(html: string)
| 256 | |
| 257 | describe("live scanning integration", () => { |
| 258 | function runAxeScan(html: string): any { |
| 259 | const fs = require("fs"); |
| 260 | const path = require("path"); |
| 261 | const tmpFile = path.join(__dirname, `..`, `.axe-scan-${Date.now()}.cjs`); |
| 262 | const script = ` |
| 263 | const { JSDOM } = require('jsdom'); |
| 264 | const axeCore = require('axe-core'); |
| 265 | const html = ${JSON.stringify(html)}; |
| 266 | const fullHtml = html.includes('<html') ? html : '<!DOCTYPE html><html lang="en"><head><title>Test</title></head><body>' + html + '</body></html>'; |
| 267 | const dom = new JSDOM(fullHtml, { runScripts: 'dangerously', pretendToBeVisual: true, url: 'http://localhost' }); |
| 268 | const s = dom.window.document.createElement('script'); |
| 269 | s.textContent = axeCore.source; |
| 270 | dom.window.document.head.appendChild(s); |
| 271 | dom.window.axe.run(dom.window.document, { rules: { 'color-contrast': { enabled: false }, 'color-contrast-enhanced': { enabled: false }, 'link-in-text-block': { enabled: false } } }).then(results => { |
| 272 | dom.window.close(); |
| 273 | console.log(JSON.stringify({ violations: results.violations.map(v => ({ id: v.id, impact: v.impact, nodes: v.nodes.length })), passes: results.passes.length })); |
| 274 | }).catch(err => { dom.window.close(); console.log(JSON.stringify({ error: err.message })); }); |
| 275 | `; |
| 276 | fs.writeFileSync(tmpFile, script); |
| 277 | try { |
| 278 | const result = execSync(`node "${tmpFile}"`, { |
| 279 | timeout: 30000, |
| 280 | encoding: "utf-8", |
| 281 | cwd: path.resolve(__dirname, ".."), |
| 282 | }).trim(); |
| 283 | return JSON.parse(result); |
| 284 | } finally { |
| 285 | fs.unlinkSync(tmpFile); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | it("should detect missing alt text on images", () => { |
| 290 | const result = runAxeScan('<img src="photo.jpg">'); |
no test coverage detected