I am trying to get into unittesting with node via Mocha and Chai. I am somewhat familiar with Python's built in unittest framework, so I am using Mocha's tdd interface, and chai's TDD style assertions.
The issue I am running into is with mocha's tdd setup function. It is running, but the variables I declare within it are undefined within the tests.
Here is my code:
test.js
var assert = require('chai').assert;
suite('Testing unit testing === testception', function(){
setup(function(){
// setup function, y u no define variables?
// these work if I move them into the individual tests,
// but that is not what I want :(
var
foo = 'bar';
});
suite('Chai tdd style assertions should work', function(){
test('True === True', function(){
var blaz = true;
assert.isTrue(blaz,'Blaz is true');
});
});
suite('Chai assertions using setup', function(){
test('Variables declared within setup should be accessible',function(done){
assert.typeOf(foo, 'string', 'foo is a string');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
});
});
});
Which generates the following error:
✖ 1 of 2 tests failed:
1) Testing unit testing === testception Chai assertions using setup Variables declared within setup should be accessible:
ReferenceError: foo is not defined
You're declaring foo in a scope that is inaccessible to other tests.
suite('Testing unit testing === testception', function(){
var foo; // declare in a scope all the tests can access.
setup(function(){
foo = 'bar'; // make changes/instantiations here
});
suite('this test will be able to use foo', function(){
test('foo is not undefined', function(){
assert.isDefined(foo, 'foo should have been defined.');
});
});
});