| 1 | 'use strict'; |
| 2 | |
| 3 | const Sequencer = require('@jest/test-sequencer').default; |
| 4 | const fs = require('fs'); |
| 5 | |
| 6 | class SizeBalancedSequencer extends Sequencer { |
| 7 | shard(tests, {shardIndex, shardCount}) { |
| 8 | const shards = Array.from({length: shardCount}, () => ({ |
| 9 | tests: [], |
| 10 | size: 0, |
| 11 | })); |
| 12 | const sorted = [...tests].sort( |
| 13 | (a, b) => fs.statSync(b.path).size - fs.statSync(a.path).size |
| 14 | ); |
| 15 | |
| 16 | for (let i = 0; i < sorted.length; i++) { |
| 17 | const test = sorted[i]; |
| 18 | const size = fs.statSync(test.path).size; |
| 19 | const smallest = shards.reduce((min, s) => (s.size < min.size ? s : min)); |
| 20 | smallest.tests.push(test); |
| 21 | smallest.size += size; |
| 22 | } |
| 23 | |
| 24 | return shards[shardIndex - 1].tests; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | module.exports = SizeBalancedSequencer; |