pestphp/pest · error · AfterBeforeTestFunction

The [after] hook may only be chained onto [beforeEach] insid

Error message

The [after] hook may only be chained onto [beforeEach] inside a [describe] block. Please move it inside one in [%s].

What it means

Error "The [after] hook may only be chained onto [beforeEach] inside a [describe] block. Please move it inside one in [%s]." thrown in pestphp/pest.

Source

Thrown at src/PendingCalls/BeforeEachCall.php:83

        $beforeEachTestCase = ChainableClosure::boundWhen(
            fn (): bool => $describing === [] || in_array(Arr::last($describing), $this->__describing, true),
            ChainableClosure::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class),
        )->bindTo($this, self::class);

        assert($beforeEachTestCase instanceof Closure);

        $this->testSuite->beforeEach->set(
            $this->filename,
            $this,
            $beforeEachTestCall,
            $beforeEachTestCase,
        );
    }

    public function after(Closure $closure): self
    {
        if ($this->describing === []) {
            throw new AfterBeforeTestFunction($this->filename);
        }

        return $this->__call('after', [$closure]);
    }

    /**
     * @param  array<int, mixed>  $arguments
     */
    public function __call(string $name, array $arguments): self
    {
        if (method_exists(TestCall::class, $name)) {
            $this->testCallProxies
                ->add(Backtrace::file(), Backtrace::line(), $name, $arguments);

            return $this;
        }

        $this->testCaseProxies

View on GitHub (pinned to 1af74a215c)

When it happens

Trigger: Thrown at src/PendingCalls/BeforeEachCall.php:83 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/90b67129578a0e4d. Report an issue: GitHub.