Created
December 14, 2022 21:40
-
-
Save amochkin/3a802c07df6c3e757c2c7679d9dc8316 to your computer and use it in GitHub Desktop.
Custom Jest sequencer Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Sequencer, { ShardOptions } from '@jest/test-sequencer'; | |
import { Test } from '@jest/test-result'; | |
const sorterFn = (a: Test, b: Test) => { | |
return a.path > b.path ? 1 : -1; | |
}; | |
class CustomSequencer extends Sequencer { | |
/** | |
* Select tests for shard requested via --shard=shardIndex/shardCount | |
* Sharding is applied before sorting | |
*/ | |
shard(tests: Array<Test>, options: ShardOptions) { | |
const { shardIndex, shardCount } = options; | |
const shardSize = Math.ceil(tests.length / shardCount); | |
const shardStart = shardSize * (shardIndex - 1); | |
const shardEnd = shardSize * shardIndex; | |
return [...tests].sort(sorterFn).slice(shardStart, shardEnd); | |
} | |
/** | |
* Sort test to determine order of execution | |
* Sorting is applied after sharding | |
*/ | |
sort(tests: Array<Test>) { | |
// Test structure information | |
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21 | |
const copyTests = Array.from(tests); | |
return copyTests.sort(sorterFn); | |
} | |
} | |
module.exports = CustomSequencer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment