Skip to content

Commit

Permalink
Update Configuration.md for testSequencer example in ts
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxdios authored Dec 15, 2024
1 parent 611d1a4 commit 7d24cf6
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7d24cf6

Please sign in to comment.