JavaScript unit testing tools Cheat Sheet
by Andrey (apk) via cheatography.com/6656/cs/6151/
Chai.js should Chai.js expect (cont) Chai.js asserts (cont)
object.should.equal(expected) expect(object).be.false assert.isArray
object.should.eql(expected) expect(object).be.null assert.isString
object.should.deep.equal(expected) // same as expect(object).be.undefined assert.isNumber
.eql
expect(object).be.empty assert.isBoolean
object.should.be.a('string')
expect(object).be.arguments assert.typeOf(/tea/, 'regexp') //
object.should.include(val) Object.prototype.toString()
expect(object).be.function
object.should.be.ok(val) expect(object).be.instanceOf assert.instanceOf(chai, Tea)
object.should.be.true assert.include([ a,b,c ], a)
expect(object).gt(5) # or .above .greaterThan
object.should.be.false assert.match(val, /regexp/)
expect(object).gte # or .at.least
object.should.be.null expect(object).lt(5) # or .below assert.property(obj, 'tea') // 'tea' in object
object.should.be.undefined assert.deepProperty(obj, 'tea.green')
expect(object).respondTo('bar')
object.should.be.empty expect(object).satisfy (n) -> n > 0 assert.propertyVal(person, 'name', 'John')
object.should.be.arguments expect(object).have.members([2, 3, 4]) assert.deepPropertyVal(post, 'author.name',
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.members([2, 3, 4]) Chai.js asserts require('chai').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
By Andrey (apk) Published 21st November, 2015. Sponsored by Readability-Score.com
cheatography.com/apk/ Last updated 17th December, 2015. Measure your website readability!
kucherenko.org/ Page 1 of 3. https://readability-score.com
JavaScript unit testing tools Cheat Sheet
by Andrey (apk) via cheatography.com/6656/cs/6151/
Sinon-chai (cont) Sinon-chai (cont) Sinon.js Spy/stub properties
expect(spy).alwaysCalledWithExactly(...args) spy.should.have.always.thrown(errorObjOrError spy
expect(spy).calledWithMatch(...args) TypeStringOrNothing) .args //=> [ [..], [..] ] one
per call
expect(spy).alwaysCalledWithMatch(...args) var sinon = require('sinon');
require('chai').use(require('sinon .thisValues
expect(spy).returned(val)
-chai')); .returnValues
expect(spy).alwaysReturned(val)
.called //=> true
expect(spy).threw(errorObjOrErrorTypeStringOr
Note that you can negate any assertion with .notCalled
Nothing)
Chai's .not. E. g. for notCalled use .callCount
expect(spy).alwaysThrew(errorObjOrErrorTypeS spy.should.have.not.been.called. .calledOnce
tringOrNothing)
.calledTwice
spy.should.have.been.called Mocha BDD
.calledThrice
spy.should.have.been.calledOnce mocha.setup('bdd'); .getCalls() //=> Array
spy.should.have.been.calledTwice describe.only('something', .getCall(0)
spy.should.have.been.calledThrice function() { .firstCall
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));
By Andrey (apk) Published 21st November, 2015. Sponsored by Readability-Score.com
cheatography.com/apk/ Last updated 17th December, 2015. Measure your website readability!
kucherenko.org/ Page 2 of 3. https://readability-score.com
JavaScript unit testing tools Cheat Sheet
by Andrey (apk) via cheatography.com/6656/cs/6151/
Sinon.js spies Sinon.js stubs
fn = sinon.spy(); stub = sinon.stub().returns(42);
fn(); stub() == 42
fn.calledOnce == true stub
fn.callCount == 1 .withArgs(42).returns(1);
.withArgs(43).throws("TypeError")
Sinon.js Spying/stubbing ;
stub
sinon.spy($, 'ajax')
.returns(1);
$.ajax();
.throws("TypeError");
$.ajax.calledOnce == true
.returnsArg(0); // Return 1st
sinon.stub($, 'ajax', function () {
argument
... }); // function optional
.callsArg(0);
$.ajax.calledWithMatch({ url: '/x'
});
$.ajax.restore();
Sinon.js mocks expectations
var mock = sinon.mock(obj);
var expectation = mock.expects("method");
expectation.atLeast(number);
expectation.atMost(number);
expectation.never();
expectation.once();
expectation.twice();
expectation.thrice();
expectation.exactly(number);
expectation.withArgs(arg1, arg2, ...);
expectation.withExactArgs(arg1, arg2, ...);
expectation.on(obj);
expectation.verify();
mock.restore();
mock.verify();
sinon.mock(jQuery).expects("ajax").
atLeast(2).atMost(5);
jQuery.ajax.verify();
By Andrey (apk) Published 21st November, 2015. Sponsored by Readability-Score.com
cheatography.com/apk/ Last updated 17th December, 2015. Measure your website readability!
kucherenko.org/ Page 3 of 3. https://readability-score.com