Skip to content

Commit

Permalink
fix: simplify .shard control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Mar 4, 2022
1 parent 1bdab88 commit edcd51b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ export default async function runJest({
}),
);

allTests = sequencer.shard?.(allTests, globalConfig.shard) ?? allTests;
allTests = globalConfig.shard
? sequencer.shard(allTests, globalConfig.shard)
: allTests;
allTests = await sequencer.sort(allTests);

if (globalConfig.listTests) {
Expand Down
23 changes: 8 additions & 15 deletions packages/jest-test-sequencer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,14 @@ export default class TestSequencer {
return cache;
}

shard(
tests: Array<Test>,
options: SortOptions = {shardCount: 1, shardIndex: 1},
): Array<Test> {
if (options.shardCount > 1) {
const shardSize = Math.ceil(tests.length / options.shardCount);
const shardStart = shardSize * (options.shardIndex - 1);
const shardEnd = shardSize * options.shardIndex;

return [...tests]
.sort((a, b) => (a.path > b.path ? 1 : -1))
.slice(shardStart, shardEnd);
}

return tests;
shard(tests: Array<Test>, options: SortOptions): Array<Test> {
const shardSize = Math.ceil(tests.length / options.shardCount);
const shardStart = shardSize * (options.shardIndex - 1);
const shardEnd = shardSize * options.shardIndex;

return [...tests]
.sort((a, b) => (a.path > b.path ? 1 : -1))
.slice(shardStart, shardEnd);
}

/**
Expand Down

0 comments on commit edcd51b

Please sign in to comment.