split-tests.js 821 B

12345678910111213141516171819202122232425262728293031323334
  1. const glob = require('glob');
  2. const fs = require('node:fs');
  3. const path = require('node:path');
  4. const currentShard = parseInt(process.argv[2], 10);
  5. const shardCount = parseInt(process.argv[3], 10);
  6. const specFiles = glob.sync('spec/*-spec.ts');
  7. const buckets = [];
  8. for (let i = 0; i < shardCount; i++) {
  9. buckets.push([]);
  10. }
  11. const testsInSpecFile = Object.create(null);
  12. for (const specFile of specFiles) {
  13. const testContent = fs.readFileSync(specFile, 'utf8');
  14. testsInSpecFile[specFile] = testContent.split('it(').length;
  15. }
  16. specFiles.sort((a, b) => {
  17. return testsInSpecFile[b] - testsInSpecFile[a];
  18. });
  19. let shard = 0;
  20. for (const specFile of specFiles) {
  21. buckets[shard].push(path.normalize(specFile));
  22. shard++;
  23. if (shard === shardCount) shard = 0;
  24. }
  25. console.log(buckets[currentShard - 1].join(' '));