Skip to content

[Bug]: --shard silently drops test files with non-Tests\ namespaces (e.g. modular app structures) #1711

Description

@Orrison

What Happened

The --shard option silently ignores test classes whose fully-qualified name does not start with Tests\. This affects any project with test files outside the standard tests/ directory — such as modular Laravel applications with test directories like app-modules/*/tests/.

Root Cause

In src/Plugins/Shard.php, the allTests() method parses --list-tests output with this regex:

preg_match_all('/ - (?:P\\\\)?(Tests\\\\[^:]+)::/', $output, $matches);

This hardcodes that the class identifier after the P\ prefix must start with Tests\. However, when tests live outside the tests/ directory, Pest generates path-based identifiers like:

 - P\Appmodules\ai\tests\Tenant\Actions\SomeTest::__pest_evaluable_...

After stripping P\, the remaining identifier starts with Appmodules\, not Tests\, so the regex never matches.

Impact

In my project:

  • 2540 total test cases in the Tenant suite
  • 189 matched by the regex (tests in tests/Tenant/)
  • 2351 silently dropped (tests in app-modules/*/tests/Tenant/)

The shard reports "16 files ran, out of 16" because it only sees 16 test files total. The remaining ~90% of the test suite is invisible to sharding — they either all run unsharded in every shard (wasting CI time) or are filtered out entirely by the --filter argument.

There is no warning or error indicating that tests were missed.

Expected Behavior

The regex should match any test class identifier from --list-tests output, not just those namespaced under Tests\. A fix would be:

preg_match_all('/ - (?:P\\\\)?([^:]+)::/', $output, $matches);

How to Reproduce

  1. Create a project with tests outside tests/ (e.g. in app-modules/module-name/tests/)
  2. Configure phpunit.xml testsuite to include those directories
  3. Run pest --testsuite=YourSuite --shard=1/2
  4. Observe the shard only reports tests from tests/ — app-module tests are missing

Sample Repository

canyongbs/advisingapp#2549

Pest Version

v4.7.0

PHP Version

8.4

Operation System

macOS

Notes

  • Pest v4.7.0
  • PHP 8.4
  • Laravel modular application with ~40 modules in app-modules/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions