I need to run some eval code inside of a mocha test in nodejs, but I keep running into an issue. I cannot do any var declarations inside of a test. So, if I have something like this:
it("7.Variable Assignment", function(done){
eval("var testVar = 1;");
expect(testVar).eql(1);
done();
});
The test returns:
7.Variable Assignment:
ReferenceError: testVar is not defined
at eval (eval at <anonymous> (test/index.js:102:16), <anonymous>:1:1)
at Context.<anonymous> (test/index.js:102:16)
But if I change it to:
it("7.Variable Assignment", function(done){
var testVar;
eval("testVar = 1;");
expect(testVar).eql(1);
done();
});
The test passes. Any ideas on how to fix this problem? EDIT: I need to do variable declarations inside the string.