split-tests.js 769 B

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