pestphp/pest · error · InvalidArgumentException
The given closure's parameter must be type-hinted as the cla
Error message
The given closure's parameter must be type-hinted as the class string.
What it means
Error "The given closure's parameter must be type-hinted as the class string." thrown in pestphp/pest.
Source
Thrown at src/Mixins/Expectation.php:827
/**
* @param (Closure(Throwable): mixed)|string $exception
* @return self<TValue>
*/
public function toThrow(callable|string|Throwable $exception, ?string $exceptionMessage = null, string $message = ''): self
{
$callback = NullClosure::create();
if ($exception instanceof Closure) {
$callback = $exception;
$parameters = new ReflectionFunction($exception)->getParameters();
if (count($parameters) !== 1) {
throw new InvalidArgumentException('The given closure must have a single parameter type-hinted as the class string.');
}
if (! ($type = $parameters[0]->getType()) instanceof ReflectionNamedType) {
throw new InvalidArgumentException('The given closure\'s parameter must be type-hinted as the class string.');
}
$exception = $type->getName();
}
try {
($this->value)();
} catch (Throwable $e) {
if ($exception instanceof Throwable) {
// @phpstan-ignore-next-line
expect($e)
->toBeInstanceOf($exception::class, $message)
->and($e->getMessage())->toBe($exceptionMessage ?? $exception->getMessage(), $message);
return $this;
}
View on GitHub (pinned to 1af74a215c)
When it happens
Trigger: Thrown at src/Mixins/Expectation.php:827 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of pestphp/pest@1af74a215c (2026-08-21).
Data as JSON: /api/errors/79d9d00be6324451.
Report an issue: GitHub.