I have a leak problem while testing my jQuery plugin. The problem arises when I want to make fun of a value or function on a literal object.
Example:
test('Overwrite some default setting', function(){
$.fn.plugin.defaults.bar = 'foo';
});
test('Bar should be undefined', function(){
equals( $.fn.plugin.defaults.bar, undefined );
});
This test fails because the first test added "bar" var to the default values. I fixed it with the following code, but the copy of the copy does not look very elegant.
$(function(){
var defaults_copy = $.extend({}, $.fn.plugin.defaults );
var setdown = {
setup : function(){
$.fn.plugin.defaults = $.extend({}, defaults_copy);
},
teardown : function(){ }
};
module('Test leakage', setdown );
test('Overwrite some default setting', function(){
$.fn.plugin.defaults.bar = 'foo';
});
test('Bar should be undefined', function(){
equals( $.fn.plugin.defaults.bar, undefined );
});
})
Also, if I have several objects in the jQuery namespace, this can become a little dirty if I need to take several copies of each object. So it was interesting if anyone has a better solution to "reset" all objects?
source
share