diff --git a/CHANGELOG.md b/CHANGELOG.md index cd34d1360be7..26041b4a48b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -122,6 +122,12 @@ - `[docs]` `Revised documentation for .toHaveBeenCalled()` to accurately depict its functionality. ([#14853](https://github.com/jestjs/jest/pull/14853)) - `[docs]` Removed ExpressJS reference link from documentation due to dead link ([#15270](https://github.com/jestjs/jest/pull/15270)) +## 29.7.1 + +### Chore & Maintenance + +- `[docs]` Added example of `testSequencer` in typescript inside configuration.md file + ## 29.7.0 ### Features diff --git a/docs/Configuration.md b/docs/Configuration.md index 1ca9a15e1341..76ec1af78e2a 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -2195,7 +2195,7 @@ Both `sort` and `shard` may optionally return a `Promise`. For example, you may sort test paths alphabetically: -```js title="custom-sequencer.js" +```js tab const Sequencer = require('@jest/test-sequencer').default; class CustomSequencer extends Sequencer { @@ -2228,6 +2228,35 @@ class CustomSequencer extends Sequencer { module.exports = CustomSequencer; ``` +```ts tab +import TestSequencer, { ShardOptions } from '@jest/test-sequencer' +import { Test } from '@jest/test-result' + +export default class CustomSequencer extends TestSequencer { + /** + * Select tests for shard requested via --shard=shardIndex/shardCount + * Sharding is applied before sorting + */ + shard (tests: Test[], { shardIndex, shardCount }: ShardOptions): any[] { + const shardSize = Math.ceil(tests.length / shardCount) + const shardStart = shardSize * (shardIndex - 1) + const shardEnd = shardSize * shardIndex + + return [...tests].sort((a, b) => (a.path > b.path ? 1 : -1)).slice(shardStart, shardEnd) + } + + /** + * Sort test to determine order of execution + * Sorting is applied after sharding + */ + sort (tests: Test[]): any[] { + const copyTests = [...tests] + return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1)) + } +} + +``` + Add `custom-sequencer` to your Jest configuration: ```js tab