On Github ajacksified / javascript-testing
Don't test jQuery, test that you're calling jQuery.
Don't test AJAX, test that you're making a request.
var className = 'active', el = { addClass: sinon.spy(); }; myModule.$el = el; myModule.activate(); expect(el.addClass).calledWith(className); // just test that it's called. // we don't need to test if jQuery set the class. // we can assume jQuery actually works.
myModule.activate(); expect(myModule.$el.hasClass("active")).to.be.true;
describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ assert.equal(-1, [1,2,3].indexOf(5)); assert.equal(-1, [1,2,3].indexOf(0)); }); }); });
var chai = require('chai'), expect = chai.expect; var Emitter = require('../src/js/emitter.js'); describe('binding with `on`', function(){ /* -- tests -- */ it('should do nothing if no event name is given', function(){ var e = new Emitter(); e.on(); expect(e.on).to.not.throw(Error); expect(e.queue).to.be.empty; }); });
sinon = require('sinon'); sinonChai = require('sinon-chai'; describe('calling events with `emit`', function(){ it('should call all the functions bound in the queue', function(){ var e = new Emitter(), fn = sinon.spy(); e.queue['test'] = [fn]; e.emit('test', 1, 2); expect(fn).calledWith(1, 2); }); });
describe 'my object', -> it 'should do a thing', -> sinon.spy(myObject', 'doSomething') myObject.doSomething(7) expect(myObject.doSomething).calledWith(7)
var myFunction = function(){ /* stuff */ }; module.exports = myFunction;
var myFunction = require('./myFunction.js'); myFunction("HI GUYS I'M DOING A THING");
!function(global){ var myFunction = function(){ /* stuff */ }; // export it using Browserify if we can if(module && module.exports){ module.exports = myFunction; // otherwise just attach it to global, which is `window` }else{ global.myFunction = myFunction; } }(this);