JavaScript Unit Testing Tools Cheat Sheet
JavaScript Unit Testing Tools Cheat Sheet
JavaScript Unit Testing Tools Cheat Sheet
object.should.be.function 'John')
expect(object).have.keys(['foo'])
object.should.be.instanceOf assert.lengthOf(object, 3)
expect(object).have.key('foo')
object.should.gt(5) # or .above .greaterThan assert.throws(function() { ... })
expect(object).exist
assert.doesNotThrow
object.should.gte # or .at.least expect(object).(-> ...).throw /not a function/
assert.operator(1, '<', 2)
object.should.lt(5) # or .below
var expect =
assert.closeTo(actual, expected)
object.should.respondTo('bar') require('chai').expect;
object.should.satisfy (n) -> n > 0 var assert =
object.should.have.keys(['foo']) assert(val)
Sinon-chai
object.should.have.key('foo') assert.fail(actual, expected)
expect(spy).called
object.should.exist assert.ok(val) // is truthy
expect(spy).calledOnce
require('chai').should(); assert.equal(actual, expected) // 'compare with
expect(spy).calledTwice
//actually call the function, add =='
expect(spy).calledThrice
"should" method to prototype of assert.strictEqual
object expect(spy).calledBefore(spy2)
assert.deepEqual
expect(spy).calledAfter(spy2)
assert.isTrue
Chai.js expect expect(spy).calledWithNew
assert.isFalse
expect(object).equal(expected) assert.isNull expect(spy).alwaysCalledWithNew
expect(object).eql(expected) expect(spy).calledOn(context)
assert.isNotNull
expect(object).deep.equal(expected) // same as assert.isUndefined expect(spy).alwaysCalledOn(context)
.eql expect(spy).calledWith(...args)
assert.isDefined
expect(object).be.a('string')
expect(spy).alwaysCalledWith(...args)
assert.isFunction
expect(object).include(val)
assert.isObject expect(spy).calledWithExactly(...args)
expect(object).be.ok(val)
expect(object).be.true
spy1.should.have.been.calledBefore(spy2) beforeEach(function() {
}); Sinon.JS Sandbox
spy1.should.have.been.calledAfter(spy2)
it.skip('should work',
spy.should.have.been.calledWithNew beforeEach(function() {
function() {
global.env =
spy.should.always.have.been.calledWithNew
});
require('sinon').sandbox.create();
spy.should.have.been.calledOn(context) it('should save',
});
spy.should.always.have.been.calledOn(context) function(done) {
afterEach(function() {
spy.should.have.been.calledWith(...args) var user = new User();
global.env.restore();
spy.should.always.have.been.calledWith(...args) user.save(function(err) {
});
if (err) throw err;
spy.should.always.have.been.calledWithExactly(..
.args) done();
Sinon.js Fake Server, XHR and date
});
spy.should.always.have.been.calledWithExactly(..
}); $.get('/file.json', ...);
.args)
server.requests[0].respond(
})
spy.should.have.been.calledWithMatch(...args)
200,
spy.should.always.have.been.calledWithMatch(... { "Content-Type":
Mocha TDD
args)
"application/json" },
spy.should.have.returned(returnVal) mocha.setup('tdd');
JSON.stringify([{ id: 1, text:
suite('something', function() {
spy.should.have.always.returned(returnVal) "Provide examples", done: true }])
setup(function() {
spy.should.have.thrown(errorObjOrErrorTypeStri );
});
ngOrNothing) server.restore();
test('should work', function() {
xhr =
});
sinon.useFakeXMLHttpRequest();
teardown(function() {
xhr.restore();
});
sinon.useFakeTimers(+new
});
Date(2011,9,1));
expectation.atLeast(number);
expectation.atMost(number);
expectation.never();
expectation.once();
expectation.twice();
expectation.thrice();
expectation.exactly(number);
expectation.on(obj);
expectation.verify();
mock.restore();
mock.verify();
sinon.mock(jQuery).expects("ajax").
atLeast(2).atMost(5);
jQuery.ajax.verify();