#2014·mocha

Bug: Contexts not properly reset between tests within the same level

Author: mollCreated Dec 17, 2015Updated Sep 20, 2026
Labelstype: bugsemver-majorstatus: in discussion

Hey,

I wouldn't be surprised if related to my years old bug report and gripe: https://github.com/mochajs/mocha/issues/1195

javascript
var assert = require("assert")

describe("Parent", function() {
  beforeEach(function() { this.value = 42 })

  it("must pass 1", function() { assert.equal(this.value, 42) })
  it("must pass 2", function() { assert.equal(this.value, 42) })

  describe("Child", function() {
    beforeEach(function() {
      assert.equal(this.value, 42)
      this.value = 13
    })

    it("must pass 1", function() { assert.equal(this.value, 13) })
    it("must pass 2", function() { assert.equal(this.value, 13) })
  })
})

Fails with:

  1) Parent Child "before each" hook for "must pass 2":

      AssertionError: 13 == 42
      + expected - actual

      -13
      +42

      at Context.<anonymous> (test/index_test.js:11:14)