pestphp/pest · error · InvalidArgumentException

The given closure must have a single parameter type-hinted a

Error message

The given closure must have a single parameter type-hinted as the class string.

What it means

Error "The given closure must have a single parameter type-hinted as the class string." thrown in pestphp/pest.

Source

Thrown at src/Mixins/Expectation.php:823

        Assert::assertContainsOnlyInstancesOf($class, $this->value, $message);

        return $this;
    }

    /**
     * @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);

View on GitHub (pinned to 1af74a215c)

When it happens

Trigger: Thrown at src/Mixins/Expectation.php:823 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/6d1cd90db7e5c089. Report an issue: GitHub.