javascript - Jasmine test not passing when function returning correct result -
have simple function returning value:
obj.id = function(id) { if (!id) { throw new error('obj.id: no param'); } else if (typeof(id) === 'string') { return obj.doc.getelementbyid(id); } else { return null; } }; running test:
it('should return null if passed parameter not string', function() { expect(function() { obj.id({}); }).tobenull(); }); test fails pass though should pass.
it('should throw error if no parameter passed in', function() { expect(function() { obj.id(); }).tothrow(new error('obj.id: no param')); }); this 1 passes.
any ideas?
you're passing {} variable id function. though object has no keys, empty object evaluates true when used in if condition
you can prove using this:
({}) ? console.log('not falsey') : console.log('falsey'); // -> outputs 'not falsey' maybe can use hasownproperty() function instead?
({}.hasownproperty()) ? console.log('not empty') : console.log('empty'); // -> outputs 'empty'
Comments
Post a Comment