run-node-tests.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env node
  2. import fs from 'fs';
  3. import path from 'path';
  4. import { spawnSync } from 'child_process';
  5. const cwd = process.cwd();
  6. const args = process.argv.slice(2);
  7. const matchIdx = args.indexOf('--match');
  8. const matchText = matchIdx >= 0 ? String(args[matchIdx + 1] || '').trim() : '';
  9. const listOnly = args.includes('--list');
  10. let tests = fs.readdirSync(cwd)
  11. .filter((file) => /^test-.*\.js$/.test(file))
  12. .sort();
  13. if (matchText) {
  14. tests = tests.filter((file) => file.includes(matchText));
  15. }
  16. if (tests.length === 0) {
  17. console.error(`No tests found${matchText ? ` for --match ${matchText}` : ''}.`);
  18. process.exit(1);
  19. }
  20. if (listOnly) {
  21. for (const file of tests) console.log(file);
  22. process.exit(0);
  23. }
  24. const failures = [];
  25. for (const file of tests) {
  26. console.log(`\n### ${file}`);
  27. const result = spawnSync(process.execPath, [path.join(cwd, file)], {
  28. cwd,
  29. encoding: 'utf8',
  30. });
  31. if (result.stdout) process.stdout.write(result.stdout);
  32. if (result.stderr) process.stderr.write(result.stderr);
  33. if (result.status !== 0) failures.push(file);
  34. }
  35. console.log(`\nSUMMARY total=${tests.length} failed=${failures.length}`);
  36. if (failures.length) {
  37. for (const file of failures) console.log(file);
  38. process.exit(1);
  39. }