test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { fileURLToPath } from 'url';
  2. import { dirname } from 'path';
  3. import { glob } from 'glob';
  4. import { runQunitPuppeteer, printFailedTests } from 'node-qunit-puppeteer';
  5. import { createServer } from 'vite';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = dirname(__filename);
  8. const testFiles = glob.sync('test/*.html');
  9. const combinedResults = { passed: 0, failed: 0, total: 0, runtime: 0 };
  10. // Create and start Vite server
  11. const startServer = async () => {
  12. const server = await createServer({
  13. root: __dirname,
  14. server: {
  15. port: 8009,
  16. },
  17. });
  18. await server.listen();
  19. return server;
  20. };
  21. // Run tests
  22. const runTests = async (server) => {
  23. await Promise.all(
  24. testFiles.map(async (file) => {
  25. const qunitArgs = {
  26. targetUrl: `http://localhost:8009/${file}`,
  27. timeout: 30000,
  28. redirectConsole: false,
  29. puppeteerArgs: ['--allow-file-access-from-files'],
  30. };
  31. try {
  32. const result = await runQunitPuppeteer(qunitArgs);
  33. combinedResults.passed += result.stats.passed;
  34. combinedResults.failed += result.stats.failed;
  35. combinedResults.total += result.stats.total;
  36. combinedResults.runtime += result.stats.runtime;
  37. if (result.stats.failed > 0) {
  38. console.log(
  39. `${'!'} ${file} [${result.stats.passed}/${result.stats.total}] in ${
  40. result.stats.runtime
  41. }ms`.red
  42. );
  43. printFailedTests(result, console);
  44. } else {
  45. console.log(
  46. `${'✔'} ${file} [${result.stats.passed}/${result.stats.total}] in ${
  47. result.stats.runtime
  48. }ms`.green
  49. );
  50. }
  51. } catch (error) {
  52. console.error(`Error running tests for ${file}:`, error);
  53. }
  54. })
  55. );
  56. console.log(
  57. `\n${combinedResults.passed}/${combinedResults.total} tests passed, ${combinedResults.failed} failed, ${combinedResults.runtime}ms runtime`
  58. );
  59. // Exit with status code 1 if any tests failed, otherwise exit with 0
  60. process.exit(combinedResults.failed > 0 ? 1 : 0);
  61. };
  62. // Main execution
  63. (async () => {
  64. try {
  65. const server = await startServer();
  66. await runTests(server);
  67. await server.close();
  68. } catch (error) {
  69. console.error('An error occurred:', error);
  70. process.exit(1);
  71. }
  72. })();