pestphp / pestphp/pest

[Bug]: AfterAll() hook not being called

Open
#1,694 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
11.7k
Forks
538
Avg merge
4d 11h
Merged PRs (30d)
8

Description

What Happened

I am trying to use the AfterAll() global hook to reset fixture data in a file.

What I expected was that the data would be reset.

What happens is nothing; the closure I set in Pest.php is never called.

If I set a breakpoint on the closure it is never reached.

By contrast, the BeforeAll() (which I use to initialise the fixture data), BeforeEach() and AfterEach() global hooks work as expected and pause on breakpoints when set.

How to Reproduce

In Pest.php
pest()->in('Feature') ->extend(TestCase::class) ->beforeAll(function () { TestCase::beforeAll(); }) ->afterAll(function () { TestCase::afterAll(); }) ;

In TestCase.php
`
abstract class TestCase extends BaseTestCase {
public static function afterAll(): void
{
// clear fixture - set a breakpoint here and it will not be hit
}

public static function beforeAll(): void
{
    // initialise fixture - set a breakpoint here and it will be hit
}

}
`

Sample Repository

No response

Pest Version

pest: 4.7 & pest-plugin-browser: 4.3.1

PHP Version

8.5.3

Operation System

Linux

Notes

Looking in Testable::tearDownAfterClass() (I assuming this is the right place as the comment states that "This method is called after the last test of this Test Case is run."), $afterAll is an instance of Support/NullClosure.php; I kind of think it should be the closure I set in Pest.php.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Testable::tearDownAfterClass(), where the issue reports that $afterAll is a Support/NullClosure instead of the closure configured in Pest.php. Trace how the global afterAll hook is registered for the Feature group and compare it with the working beforeAll, beforeEach, and afterEach hooks. Done means the closure in TestCase.php is called after the last test and its breakpoint is reached.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.