Fix an issue where beforeEach/afterEach functions were called on other describe blocks with the same name

This commit is contained in:
jshayes
2024-10-12 01:06:16 -04:00
parent 709ecb1ba2
commit b5b8fab09b
17 changed files with 1443 additions and 86 deletions

View File

@ -51,3 +51,78 @@ describe('with expectations', function () {
test('test', function () {});
});
describe('matching describe block names', function () {
beforeEach(function () {
$this->foo = 1;
});
describe('outer', function () {
beforeEach(function () {
$this->foo++;
});
describe('middle', function () {
beforeEach(function () {
$this->foo++;
});
describe('inner', function () {
beforeEach(function () {
$this->foo++;
});
it('should call all parent beforeEach functions', function () {
expect($this->foo)->toBe(4);
});
});
});
describe('middle', function () {
it('should not call beforeEach functions for sibling describe blocks with the same name', function () {
expect($this->foo)->toBe(2);
});
});
describe('inner', function () {
it('should not call beforeEach functions for descendent of sibling describe blocks with the same name', function () {
expect($this->foo)->toBe(2);
});
});
});
});
$matchingNameCalls = 0;
describe('matching name', function () use (&$matchingNameCalls) {
beforeEach(function () use (&$matchingNameCalls) {
$matchingNameCalls++;
});
it('should call the before each', function () use (&$matchingNameCalls) {
expect($matchingNameCalls)->toBe(1);
});
});
describe('matching name', function () use (&$matchingNameCalls) {
it('should not call the before each on the describe block with the same name', function () use (&$matchingNameCalls) {
expect($matchingNameCalls)->toBe(1);
});
});
beforeEach(function () {
$this->baz = 1;
});
describe('called on all tests', function () {
beforeEach(function () {
$this->baz++;
});
test('beforeEach should be called', function () {
expect($this->baz)->toBe(2);
});
test('beforeEach should be called for all tests', function () {
expect($this->baz)->toBe(2);
});
});