Skip to content

[Bug]: Datasets with only one named parameter result in tests failing #1868

Description

@parapente

What Happened

Following the pest docs about named parameters in datasets, I had setup some tests with a dataset that uses an associative array with only one key. The tests were running correctly in pest version 2, 3 and 4 but are now failing to run after upgrading to pest 5. It seems that pest handles arrays with a count of 1 as an indexed array and then this line https://github.com/sonalidudhia/pest/blob/974546d49f3813456826ee6e58bc611812f9b52d/src/Factories/TestCaseMethodFactory.php#L238 tries to read array index 0 and fails.

How to Reproduce

You can try to following simple example:

it('has user data', function (array $data) {
    expect($data['name'])->toBeString();
    expect($data['email'])->toContain('@');
})->with([
    ['data' => ['name' => 'Taylor', 'email' => 'taylor@laravel.com']],
    ['data' => ['name' => 'Nuno', 'email' => 'enunomaduro@gmail.com']],
]);

Execution:

php artisan test tests/Feature/MiniTest.php                                                                                                  776ms

   FAIL  Tests\Feature\MiniTest
  ⨯ it has user data with (['Taylor', 'taylor@laravel.com'])                                                                           0.10s  
  ⨯ it has user data with (['Nuno', 'enunomaduro@gmail.com'])                                                                          0.03s  
  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  
   FAILED  Tests\Feature\MiniTest > it has user data with (['Taylor', 'taylor@laravel.com'])                                 ErrorException   
  Undefined array key 0

  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  
   FAILED  Tests\Feature\MiniTest > it has user data with (['Nuno', 'enunomaduro@gmail.com'])                                ErrorException   
  Undefined array key 0


  Tests:    2 failed (0 assertions)
  Duration: 0.19s

Sample Repository

No response

Pest Version

5.1.1

PHP Version

8.4.20

Operation System

Linux

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions