| 1 | import { fileURLToPath } from 'url'; |
| 2 | import { dirname, resolve } from 'path'; |
| 3 | import { glob } from 'glob'; |
| 4 | import { runQunitPuppeteer, printFailedTests } from 'node-qunit-puppeteer'; |
| 5 | import { createServer } from 'vite'; |
| 6 | |
| 7 | const __filename = fileURLToPath(import.meta.url); |
| 8 | const __dirname = dirname(__filename); |
| 9 | const root = resolve(__dirname, '..'); |
| 10 | |
| 11 | const testFiles = glob.sync('test/*.html', { cwd: root }); |
| 12 | const isCI = process.env.CI === 'true' || process.env.CI === '1'; |
| 13 | const puppeteerArgs = [ |
| 14 | '--allow-file-access-from-files', |
| 15 | ...( isCI ? [ '--no-sandbox', '--disable-setuid-sandbox' ] : [] ) |
| 16 | ]; |
| 17 | |
| 18 | const combinedResults = { passed: 0, failed: 0, total: 0, runtime: 0, errors: 0 }; |
| 19 | |
| 20 | // Create and start Vite server (root = project root) |
| 21 | const startServer = async () => { |
| 22 | const server = await createServer({ |
| 23 | root, |
| 24 | server: { |
| 25 | port: 8009 |
| 26 | }, |
| 27 | }); |
| 28 | await server.listen(); |
| 29 | |
| 30 | const baseUrl = server.resolvedUrls?.local?.[0] || 'http://127.0.0.1:8009/'; |
| 31 | return { server, baseUrl }; |
| 32 | }; |
| 33 | |
| 34 | // Run tests |
| 35 | const runTests = async (baseUrl) => { |
| 36 | await Promise.all( |
| 37 | testFiles.map(async (file) => { |
| 38 | const qunitArgs = { |
| 39 | targetUrl: new URL(file, baseUrl).href, |
| 40 | timeout: 30000, |
| 41 | redirectConsole: false, |
| 42 | puppeteerArgs, |
| 43 | }; |
| 44 | |
| 45 | try { |
| 46 | const result = await runQunitPuppeteer(qunitArgs); |
| 47 | combinedResults.passed += result.stats.passed; |
| 48 | combinedResults.failed += result.stats.failed; |
| 49 | combinedResults.total += result.stats.total; |
| 50 | combinedResults.runtime += result.stats.runtime; |
| 51 | |
| 52 | if (result.stats.failed > 0) { |
| 53 | console.log( |
| 54 | `${'!'} ${file} [${result.stats.passed}/${result.stats.total}] in ${ |
| 55 | result.stats.runtime |
| 56 | }ms`.red |
| 57 | ); |
| 58 | printFailedTests(result, console); |
| 59 | } else { |
| 60 | console.log( |
| 61 | `${'✔'} ${file} [${result.stats.passed}/${result.stats.total}] in ${ |
| 62 | result.stats.runtime |
| 63 | }ms`.green |
| 64 | ); |
| 65 | } |
| 66 | } catch (error) { |
| 67 | combinedResults.errors += 1; |
| 68 | console.error(`Error running tests for ${file}:`, error); |
| 69 | } |
| 70 | }) |
| 71 | ); |
| 72 | |
| 73 | console.log( |
| 74 | `\n${combinedResults.passed}/${combinedResults.total} tests passed, ${combinedResults.failed} failed, ${combinedResults.errors} errors, ${combinedResults.runtime}ms runtime` |
| 75 | ); |
| 76 | |
| 77 | return combinedResults.failed > 0 || combinedResults.errors > 0 ? 1 : 0; |
| 78 | }; |
| 79 | |
| 80 | // Main execution |
| 81 | (async () => { |
| 82 | let server; |
| 83 | |
| 84 | try { |
| 85 | const startedServer = await startServer(); |
| 86 | server = startedServer.server; |
| 87 | |
| 88 | process.exitCode = await runTests(startedServer.baseUrl); |
| 89 | } catch (error) { |
| 90 | console.error('An error occurred:', error); |
| 91 | process.exitCode = 1; |
| 92 | } finally { |
| 93 | if (server) { |
| 94 | await server.close(); |
| 95 | } |
| 96 | } |
| 97 | })(); |
| 98 |