Skip to content

Instantly share code, notes, and snippets.

@davetapley
Created April 10, 2013 17:45
Show Gist options
  • Save davetapley/5356831 to your computer and use it in GitHub Desktop.
Save davetapley/5356831 to your computer and use it in GitHub Desktop.
Trouble using for in loop with mocha
/* Mocha test
to use:
npm install mocha
mocha <filename>
or
npm test
*/
var assert = require('assert');
var tzwhere = require('../lib/index');
var util = require('util');
// Expected format for locations.json: [utc_time, lat, lng, time_zone_offset]
var testLocations = require('./locations.json');
describe('Test locations', function () {
//var startTime = Date().getTime();
console.time('tzOffsetAt test locations');
//var testLocation = testLocations[0];
for(var testLocation in testLocations) {
it('should properly determine the times on either side of daylight savings', function (done) {
var testDate = new Date(testLocation[0])
var testLat = testLocation[1];
var testLng = testLocation[2];
var testOffset = testLocation[3];
var tzwhereOffset = tzwhere.tzOffsetAt(testLat, testLng, testDate) / 1000;
util.debug(util.inspect(testLocation));
util.debug(util.inspect(testOffset));
util.debug(util.inspect(tzwhereOffset));
assert.equal(tzwhereOffset, testOffset);
return done();
});
}
//var endTime = Date().getTime();
console.log(testLocations.length + ' locations tested');
console.timeEnd('tzOffsetAt test locations');
after(function () {
console.log(util.inspect(process.memoryUsage()));
});
});
@davetapley
Copy link
Author

This fails.
But if I comment out lines 21 and 37, and comment in line 20 (i.e. skip the loop of testLocations and just use the zeroth element via [0]) then it works.
It still fails when I reduce the contents of ./locations.json to a single element, so I don't believe it's an issue with extra data.

@tj
Copy link

tj commented Apr 10, 2013

this is just a normal function scoping issue, you can try doing something like:

for (var foo ...) {
  (function(foo){

  })(foo)
}

then it becomes scoped to that function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment