Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Exceptions/DatasetProviderError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Pest\Exceptions;

use RuntimeException;
use Throwable;

/**
* @internal
*/
final class DatasetProviderError extends RuntimeException
{
public function __construct(Throwable $previous)
{
parent::__construct($previous->getMessage(), (int) $previous->getCode(), $previous);
}
}
1 change: 1 addition & 0 deletions src/Factories/TestCaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public function evaluate(string $filename, array $methods): void
$classCode = <<<PHP
namespace $namespace;

use Pest\Exceptions\DatasetProviderError as __PestDatasetProviderError;
use Pest\Repositories\DatasetsRepository as __PestDatasets;
use Pest\TestSuite as __PestTestSuite;

Expand Down
10 changes: 9 additions & 1 deletion src/Factories/TestCaseMethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public function buildForEvaluation(): string
$attributesCode
public function $methodName(...\$arguments)
{
if (count(\$arguments) === 1 && \$arguments[0] instanceof __PestDatasetProviderError) {
throw \$arguments[0]->getPrevious() ?? \$arguments[0];
}

return \$this->__runTest(
\$this->__test,
...\$arguments,
Expand All @@ -261,7 +265,11 @@ private function buildDatasetForEvaluation(string $methodName, string $dataProvi

public static function $dataProviderName()
{
return __PestDatasets::get(self::\$__filename, "$methodName");
try {
return __PestDatasets::get(self::\$__filename, "$methodName");
} catch (\Throwable \$throwable) {
return [[new __PestDatasetProviderError(\$throwable)]];
}
}

EOF;
Expand Down
9 changes: 9 additions & 0 deletions tests/.tests/DatasetClosureThrows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

dataset('throws', function () {
throw new RuntimeException('boom from dataset');
});

it('x', function ($a) {
expect($a)->toBeTrue();
})->with('throws');
7 changes: 7 additions & 0 deletions tests/.tests/IssueOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

it('references a missing dataset', function ($value) {
expect($value)->toBeTruthy();
})->with('missing');
11 changes: 11 additions & 0 deletions tests/.tests/IssueWithPassing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

it('passes normally', function () {
expect(true)->toBeTrue();
});

it('references a missing dataset', function ($value) {
expect($value)->toBeTruthy();
})->with('missing');
49 changes: 49 additions & 0 deletions tests/Features/DatasetProviderErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Symfony\Component\Process\Process;

$run = function (string $target): array {
$process = new Process(
['php', 'bin/pest', $target],
dirname(__DIR__, 2),
['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true'],
);

$process->run();

return [
'output' => removeAnsiEscapeSequences($process->getOutput().$process->getErrorOutput()),
'code' => $process->getExitCode(),
];
};

test('reports missing datasets as errors for a single file run', function () use ($run) {
$result = $run('tests/.tests/IssueOnly.php');

expect($result['output'])
->toContain("A dataset with the name `missing` does not exist. You can create it using `dataset('missing', ['a', 'b']);`.")
->toContain('FAILED')
->toContain('Tests: 1 failed');

expect($result['code'])->not->toBe(0);
})->skipOnWindows();

test('reports missing datasets as errors alongside passing tests', function () use ($run) {
$result = $run('tests/.tests/IssueWithPassing.php');

expect($result['output'])
->toContain("A dataset with the name `missing` does not exist. You can create it using `dataset('missing', ['a', 'b']);`.")
->toContain('1 passed')
->toContain('1 failed');

expect($result['code'])->not->toBe(0);
})->skipOnWindows();

test('reports dataset closure exceptions as errors', function () use ($run) {
$result = $run('tests/.tests/DatasetClosureThrows.php');

expect($result['output'])
->toContain('boom from dataset');

expect($result['code'])->not->toBe(0);
})->skipOnWindows();