dimanche 2 août 2015

Variable Scope Within "describe" and "it" Statements

Below is the code to run a series of tests from a loop. It's producing some unexpected behavior. It seems that the scope of the args from the first operation are leaking into the second. To put another way, for some reason callback exists within the args for the second operation.

describe("symlinkType", function() {
  tests.success.forEach(function (test) {
    var args = test[0]
    var expectedType = test[1]
    var should = util.format('should return \'%s\' when src \'%s\'', expectedType, args[0])
    it(should, function (done) {
      var callback = function (err, type) {
        if(err) done(err)
        expect(type).to.equal(expectedType)
        done()
      }
      args.push(callback)
      return symlinkType.apply(null, args)
    })
  })
})

describe("symlinkTypeSync", function()  {
  tests.success.forEach(function (test) {
    var args = test[0]
    var expectedType = test[1]
    var should = util.format('should return \'%s\' when src \'%s\'', expectedType, args[0])
    it(should, function () {
      var value = symlinkTypeSync.apply(null, args)
      expect(value).to.equal(expectedType)
    })
  })
})

I'd like a reason for this behavior and how to fix it. I'm used to when you declare a variable it changing the value of the variable and the subsequent .push() calls shouldn't be effecting the args variable in the second statement. Is this mocha's fault, is the it statement leaking scope?

Aucun commentaire:

Enregistrer un commentaire