pestphp/pest · error · ExpectationFailedException

Failed asserting that an array has the key [$key]

Error message

Failed asserting that an array has the key [$key]

What it means

Error "Failed asserting that an array has the key [$key]" thrown in pestphp/pest.

Source

Thrown at src/Mixins/Expectation.php:547

     */
    public function toHaveKey(string|int $key, mixed $value = new Any, string $message = ''): self
    {
        if (is_object($this->value) && method_exists($this->value, 'toArray')) {
            $array = $this->value->toArray();
        } else {
            $array = (array) $this->value;
        }

        try {
            Assert::assertTrue(Arr::has($array, $key));

            /* @phpstan-ignore-next-line */
        } catch (ExpectationFailedException $exception) {
            if ($message === '') {
                $message = "Failed asserting that an array has the key [$key]";
            }

            throw new ExpectationFailedException($message, $exception->getComparisonFailure());
        }

        if (! $value instanceof Any) {
            Assert::assertEquals($value, Arr::get($array, $key), $message);
        }

        return $this;
    }

    /**
     * @param  array<int, int|string|array<array-key, mixed>>  $keys
     * @return self<TValue>
     */
    public function toHaveKeys(array $keys, string $message = ''): self
    {
        foreach ($keys as $k => $key) {
            if (is_array($key)) {
                $this->toHaveKeys(array_keys(Arr::dot($key, $k.'.')), $message);

View on GitHub (pinned to 1af74a215c)

When it happens

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