pest icon indicating copy to clipboard operation
pest copied to clipboard

[Request/Help] Ability to access the value of `--filter` when generating datasets?

Open maples3 opened this issue 9 months ago • 2 comments

Hi! My team uses Pest for integration testing. We have some tests that take a while to generate the datasets for. This is slow/cumbersome when testing iteratively with Pest's --filter option, as the dataset generation happens before the filter is applied.

I've looked at the docs for filtering, datasets, and optimizing tests, but I didn't see anything that looks like it allows dataset functions to inspect the filter themselves.

Does such functionality exist?

Here is an example of what I'm trying to accomplish (though I'd be open to other ways too):

it('works properly for each dataset', function (string $input) {
    expect(...);
})->with(function () {
    // utilize filters here
});

In my case, I can probably hack around it for now by checking $_SERVER['argv'] for the presence of --filter (I'm okay with a "all-or-nothing" in this particular instance), but I'd like to know if there's a better way to go about it.

maples3 avatar Mar 11 '25 19:03 maples3

The only workaround I've found thus far is to comment out the other scenarios provided by the dataset.

For what it's worth, filtering based on dataset names is something that works with PHPUnit, so I see this as a regression when converting over to Pest.

tylernathanreed avatar Mar 26 '25 15:03 tylernathanreed

Here is my hacky workaround for detecting the filter, this is at the top of my data provider function before it starts its slow data-gathering process.

    // hack to make localhost iteration faster: if a manual filter is specified, don't generate the datasets for this test
    $commandLineContainsFilterArg = !empty(array_filter(
        $_SERVER['argv'],
        fn(string $arg) => str_starts_with($arg, '--filter')
    ));
    if ($commandLineContainsFilterArg) {
        return [];
    }

A word of warning, this workaround is broken when upgrading to Pest 3.x due to the bug I just created above, Pest no longer allows you to provide an empty dataset (and there's currently a bug causing it to emit a misleading error message instead of the intended "Empty data set provided by data provider" error).

maples3 avatar Apr 07 '25 18:04 maples3

fixed on 4.x

nunomaduro avatar Jul 30 '25 21:07 nunomaduro