Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Runnable.globals(arr) method for per test global whitelist #1046

Merged
merged 1 commit into from
Dec 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ Runnable.prototype.resetTimeout = function(){
}, ms);
};

/**
* Whitelist these globals for this test run
*
* @api private
*/
Runnable.prototype.globals = function(arr){
var self = this;
this._allowedGlobals = arr;
};

/**
* Run the test and invoke `fn(err)`.
*
Expand Down
5 changes: 5 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,15 @@ Runner.prototype.globals = function(arr){
Runner.prototype.checkGlobals = function(test){
if (this.ignoreLeaks) return;
var ok = this._globals;

var globals = this.globalProps();
var isNode = process.kill;
var leaks;

if (test) {
ok = ok.concat(test._allowedGlobals || []);
}

// check length - 2 ('errno' and 'location' globals)
if (isNode && 1 == ok.length - globals.length) return;
else if (2 == ok.length - globals.length) return;
Expand Down
10 changes: 10 additions & 0 deletions test/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ describe('Runnable(title, fn)', function(){
})
})

describe('#globals', function(){
it('should allow for whitelisting globals', function(done){
var test = new Runnable('foo', function(){});
test.async.should.be.equal(0);
test.sync.should.be.true;
test.globals(['foobar']);
test.run(done);
})
})

describe('.run(fn)', function(){
describe('when .pending', function(){
it('should not invoke the callback', function(done){
Expand Down
33 changes: 33 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ describe('Runner', function(){
});
runner.checkGlobals('im a test');
})

it('should respect per test whitelisted globals', function() {
var test = new Test('im a test about lions');
test.globals(['foo']);

suite.addTest(test);
var runner = new Runner(suite);

global.foo = 'bar';

// verify the test hasn't failed.
runner.checkGlobals(test);
test.should.not.have.key('state');

delete global.foo;
})

it('should respect per test whitelisted globals but still detect other leaks', function(done) {
var test = new Test('im a test about lions');
test.globals(['foo']);

suite.addTest(test);

global.foo = 'bar';
global.bar = 'baz';
runner.on('fail', function(test, err){
test.title.should.equal('im a test about lions');
err.message.should.equal('global leak detected: bar');
delete global.foo;
done();
});
runner.checkGlobals(test);
})
})

describe('.fail(test, err)', function(){
Expand Down