Skip to content

Test Sharding #5075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Test Sharding #5075

wants to merge 4 commits into from

Conversation

amerryma
Copy link

PR Checklist

Overview

This strategy uses file-based sharding. The entire set of test files is divided into different groups or "shards". Each shard contains a subset of the test files. The shards use a round robin approach using a simple modulus calculation. The way this works in the code is it simply filters out the tests by the shard value before proceeding to the rest of the mocha code.

A shard option has been added to the command line interface for specifying which shard to run and how many shards there are in total. This value is validated to ensure that bad values aren't inputted.

Sharding does not work when parallel mode is enabled.

Copy link

linux-foundation-easycla bot commented Jan 11, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@JoshuaKGoldberg
Copy link
Member

Marking as draft pending discussion in #4958.

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as draft March 4, 2024 17:51
@JoshuaKGoldberg JoshuaKGoldberg added the status: blocked Waiting for something else to be resolved label Mar 4, 2024
@voxpelli voxpelli requested a review from Copilot June 12, 2025 07:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds file-based test sharding support, including a new CLI --shard option, parsing/validation utilities, and filtering of test files by shard.
Key changes:

  • Introduce parseShardString and createInvalidShardError for shard validation.
  • Extend Mocha API with .shard() and wire the option through CLI and file collection.
  • Add integration tests and a new JSON assertion for validating suite counts per shard.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/integration/options/shard.spec.js New integration test covering valid/invalid shard use
test/integration/fixtures/options/*.js Fixture files defining suites for alpha, beta, theta
test/assertions.js Add .to have suite count JSON assertion
lib/utils.js Add parseShardString utility with error creation
lib/mocha.js Add .shard() API and chain it into constructor
lib/errors.js Define createInvalidShardError and INVALID_SHARD
lib/cli/run.js Wire in --shard, validate mutual exclusion with parallel
lib/cli/run-option-metadata.js Include shard in recognized option types
lib/cli/run-helpers.js Add validateShardString and pass shard to runner
lib/cli/collect-files.js Filter collected files by shard index
Comments suppressed due to low confidence (3)

lib/mocha.js:175

  • Duplicate JSDoc entry for options.slow; remove one to avoid confusion.
* @param {number} [options.slow] - Slow threshold value.

lib/errors.js:106

  • Typo in comment: change "A a shard value is invalid" to "A shard value is invalid" or similar.
*   * A a shard value is invalid

lib/cli/run.js:235

  • [nitpick] Consider adding type: 'string' to the shard option in yargs to ensure it’s parsed correctly as a string.
shard: {

Comment on lines +659 to +660
const desiredShard = parseInt(shardParts[0]);
const totalShards = parseInt(shardParts[1]);
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always specify a radix when using parseInt; e.g., use parseInt(shardParts[0], 10) and parseInt(shardParts[1], 10) to avoid unexpected behavior.

Suggested change
const desiredShard = parseInt(shardParts[0]);
const totalShards = parseInt(shardParts[1]);
const desiredShard = parseInt(shardParts[0], 10);
const totalShards = parseInt(shardParts[1], 10);

Copilot uses AI. Check for mistakes.

'--shard',
shard
];
// Test that come in through --file are always run first
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Minor grammar: consider "Tests specified via --file are always run first" or "Test files coming in through --file are always run first".

Suggested change
// Test that come in through --file are always run first
// Tests specified via `--file` are always run first

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: blocked Waiting for something else to be resolved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Feature: Test Sharding for CI Matrix Purposes
2 participants