Skip to content

Instantly share code, notes, and snippets.

@amochkin
Created December 14, 2022 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amochkin/3a802c07df6c3e757c2c7679d9dc8316 to your computer and use it in GitHub Desktop.
Save amochkin/3a802c07df6c3e757c2c7679d9dc8316 to your computer and use it in GitHub Desktop.
Custom Jest sequencer Typescript
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