Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context in before differs when it's in a nested describe vs top level #2165

Closed
ORESoftware opened this issue Mar 23, 2016 · 3 comments
Closed

Comments

@ORESoftware
Copy link

Hey all, I am trying to wrap my head around the contexts in the before hooks.

One discrepancy I noticed was the following

screenshot 2016-03-22 21 41 28

screenshot 2016-03-22 21 41 50

As you can see, for the first before hook in the top-level describe "currentTest" is undefined as we would probably expect, since before() hooks don't pertain to a particular test.

But what I didn't except was in the nested describe, the before hook 'this' value has "currentTest" defined. When I inspected with the debugger I could verify this. Why is this the case? Perhaps explaining this might help me understand Mocha contexts. Thanks!

@ORESoftware
Copy link
Author

here is the original test code that generated the above:

describe('a',function(){


    before(function(){

        console.log('before this a:',this.parent);

    });

    beforeEach(function(){

        console.log('beforeEach this a:',this.parent);

    });


    it('a', function(done){

        console.log('it this a:', this.parent);

        done();

    });


    describe('b',function(){

        before(function(){

            console.log('before this b:',this.parent);

        });

        beforeEach(function(){

            console.log('beforeEach this b:',this.parent);

        });


        it('b', function(done){

            console.log('it this b:',this.parent);


            done();
        });



    });


});

@danielstjules
Copy link
Contributor

Thanks for reporting this! Yup, this is a bug. A smaller reproduce case:

describe('foo', function() {
  it('test1', function() {});

  describe('nested', function() {
    before(function() {
      console.log('CURRENT:', this.currentTest.title);
    });

    it('test2', function() {});
  });
});
  foo
    ✓ test1
    nested
CURRENT: test1
      ✓ test2


  2 passing (8ms)

This is a known issue here: #1638

@ORESoftware
Copy link
Author

thanks, yeah np I just wanted to make sure because that would throw me way off in understanding what was going on, that's funny because I commented on #1638 without really seeing the bug myself, another software dejavu moment LOL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants