Skip to content

Commit

Permalink
Reverse afterEach/afterAll hooks execution order.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Mar 13, 2019
1 parent 6c14e95 commit 439415b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
76 changes: 76 additions & 0 deletions spec/Suite/Suite.spec.php
Expand Up @@ -137,6 +137,82 @@

});

it("runs nested `afterEach()` in the correct order", function () {

$order = [];

$describe = $this->root->describe("", function () use (&$order) {

$this->afterEach(function () use (&$order) {
$order[] = 'level1';
});

$this->describe("describe", function () use (&$order) {

$this->afterEach(function () use (&$order) {
$order[] = 'level2a';
});

$this->afterEach(function () use (&$order) {
$order[] = 'level2b';
});

$this->it("it1", function () {
expect(true)->toBe(true);
});

$this->it("it2", function () {
expect(true)->toBe(true);
});

});

});

$this->suite->run();

expect($order)->toBe(['level2b', 'level2a', 'level1', 'level2b', 'level2a', 'level1']);

});

it("runs nested `afterAll()` in the correct order", function () {

$order = [];

$describe = $this->root->describe("", function () use (&$order) {

$this->afterAll(function () use (&$order) {
$order[] = 'level1';
});

$this->describe("describe", function () use (&$order) {

$this->afterAll(function () use (&$order) {
$order[] = 'level2a';
});

$this->afterAll(function () use (&$order) {
$order[] = 'level2b';
});

$this->it("it1", function () {
expect(true)->toBe(true);
});

$this->it("it2", function () {
expect(true)->toBe(true);
});

});

});

$this->suite->run();

expect($order)->toBe(['level2b', 'level2a', 'level1']);

});

it("reports errors occuring in describes", function () {

skipIf(defined('HHVM_VERSION') || PHP_MAJOR_VERSION < 7);
Expand Down
7 changes: 5 additions & 2 deletions src/Block/Group.php
Expand Up @@ -281,7 +281,7 @@ public function beforeAll($closure)
*/
public function afterAll($closure)
{
$this->_callbacks['afterAll'][] = $this->_bindScope($closure);
array_unshift($this->_callbacks['afterAll'], $this->_bindScope($closure));
return $this;
}

Expand All @@ -307,7 +307,7 @@ public function beforeEach($closure)
*/
public function afterEach($closure)
{
$this->_callbacks['afterEach'][] = $this->_bindScope($closure);
array_unshift($this->_callbacks['afterEach'], $this->_bindScope($closure));
return $this;
}

Expand Down Expand Up @@ -392,6 +392,9 @@ protected function _blockEnd($runAfterAll = true)
public function runCallbacks($name, $recursive = true)
{
$instances = $recursive ? $this->parents(true) : [$this];
if (strncmp($name, 'after', 5) === 0) {
$instances = array_reverse($instances);
}
foreach ($instances as $instance) {
foreach ($instance->_callbacks[$name] as $closure) {
$this->_suite->runBlock($this, $closure, $name);
Expand Down

0 comments on commit 439415b

Please sign in to comment.