javascript - Mocha 'this' in before and beforeEach hooks - Stack Overflow

The following test code, written using Mocha.js fails. I expect the someVal to be increased 3 times and

The following test code, written using Mocha.js fails. I expect the someVal to be increased 3 times and equal 3 in the last test. The issue came up in more plex scenario where I used value set in outer before block to set up another in inner beforeEach block. Simplified case:

describe('increasing 3 times', function() {
  before(function() {
    this.instanceThis = this;
    return this.someVal = 0;
  });
  beforeEach(function() {
    return this.someVal += 1;
  });
  return describe('going deeper', function() {
    before(function() {
      return this.someVal += 1;
    });
    beforeEach(function() {
      return this.someVal += 1;
    });
    return it('has increased someVal to 3', function() {
      return this.someVal.should.equal(3);
    });
  });
});

The following test code, written using Mocha.js fails. I expect the someVal to be increased 3 times and equal 3 in the last test. The issue came up in more plex scenario where I used value set in outer before block to set up another in inner beforeEach block. Simplified case:

describe('increasing 3 times', function() {
  before(function() {
    this.instanceThis = this;
    return this.someVal = 0;
  });
  beforeEach(function() {
    return this.someVal += 1;
  });
  return describe('going deeper', function() {
    before(function() {
      return this.someVal += 1;
    });
    beforeEach(function() {
      return this.someVal += 1;
    });
    return it('has increased someVal to 3', function() {
      return this.someVal.should.equal(3);
    });
  });
});
Share Improve this question asked Dec 8, 2014 at 12:44 Madis NõmmeMadis Nõmme 1,3132 gold badges15 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Explanation

I don't know of any version of Mocha that would run the code you show in your question without error. For your code to work, it would have to be written like this:

require("chai").should();

describe('increasing 3 times', function() {
    before(function() {
        this.someVal = 0;
    });
    beforeEach(function() {
        this.someVal += 1;
    });
    describe('going deeper', function() {
        var parent_ctx = this.parent.ctx;
        before(function() {
            parent_ctx.someVal += 1;
            // The line above is equivalent to this:
            // this.test.parent.ctx.someVal += 1;
        });
        beforeEach(function() {
            parent_ctx.someVal += 1;
        });
        it('has increased someVal to 3', function() {
            parent_ctx.someVal.should.equal(3);
            // The above line is equivalent to this:
            // this.test.parent.parent.ctx.someVal.should.equal(3);
        });
    });
});

Inside the function passed to describe and the functions passed to the hooks of a describe block (before, beforeAll, etc.) the value of this is a "context" object which is the same for the describe and all of its own hooks (not the hooks of other describe calls nested in it). So when you assign to this, it assigns to the context. If you want to access this context inside nested calls to describe or in tests you have to walk up the tree of describe and test objects.

Solution

I would do it exactly the same way Second Rikudo suggested: use a variable scoped to your uppermost describe call. For the sake of pleteness:

require("chai").should();

describe('increasing 3 times', function() {
    var someVal;
    before(function() {
        someVal = 0;
    });
    beforeEach(function() {
        someVal += 1;
    });
    describe('going deeper', function() {
        before(function() {
            someVal += 1;
        });
        beforeEach(function() {
            someVal += 1;
        });
        it('has increased someVal to 3', function() {
            someVal.should.equal(3);
        });
    });
});

this is not what you think it is. this gets redefined in every function() {} block (unless Mocha calls them in some specific way I'm not familiar with).

What you want is to use scope in your advantage:

describe('increasing 3 times', function() {
  var someVal; // Initialization.
  before(function() {
    someVal = 0; //No need to return from before()
  });
  beforeEach(function() {
    someVal += 1;
  });
  describe('going deeper', function() {
    before(function() {
      someVal += 1;
    });
    beforeEach(function() {
      someVal += 1;
    });
    return it('has increased someVal to 3', function() {
      someVal.should.equal(3);
    });
  });
});

Also, you don't need to return so much. In fact, you almost never have to return in test code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743600391a4477022.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信