JavaScript unit testing tools Cheat Sheet
by Andrey (apk) via [Link]/6656/cs/6151/
[Link] should [Link] expect (cont) [Link] asserts (cont)
[Link]ould.equal(expected) expect(object).be.false [Link]Array
[Link]ould.eql(expected) expect(object).b[Link] [Link]String
[Link]ould.d[Link]ual(expected) // same as expect(object).be.undefined [Link]Number
.eql
expect(object).be.empty [Link]Boolean
[Link]ould.be.a('string')
expect(object).be.arguments [Link]peOf(/tea/, 'regexp') //
[Link]ould.include(val) [Link]ototype.toString()
expect(object).be.function
[Link]ould.be.ok(val) expect(object).be.instanceOf [Link]stanceOf(chai, Tea)
[Link]ould.b[Link] [Link]clude([ a,b,c ], a)
expect(object).gt(5) # or .above .greaterThan
[Link]ould.b[Link] [Link]tch(val, /regexp/)
expect(object).gte # or .[Link]
[Link]ould.b[Link] expect(object).lt(5) # or .below [Link]operty(obj, 'tea') // 'tea' in object
[Link]ould.be.undefined [Link]epProperty(obj, 'tea.green')
expect(object).respondTo('bar')
[Link]ould.b[Link] expect(object).satisfy (n) -> n > 0 [Link]opertyVal(person, 'name', 'John')
[Link]ould.be.arguments expect(object).have.members([2, 3, 4]) [Link]epPropertyVal(post, 'author.name',
[Link]ould.be.function 'John')
expect(object).have.keys(['foo'])
[Link]ould.be.instanceOf [Link]ngthOf(object, 3)
expect(object).have.key('foo')
[Link]oul[Link](5) # or .above .greaterThan [Link]rows(function() { ... })
expect(object).exist
[Link]esNotThrow
[Link]oul[Link] # or .[Link] expect(object).(-> ...).throw /not a function/
[Link]erator(1, '<', 2)
[Link]oul[Link](5) # or .below
var expect =
[Link]oseTo(actual, expected)
[Link]ould.respondTo('bar') require('chai').expect;
[Link]ould.satisfy (n) -> n > 0 var assert =
[Link]ould.h[Link]mbers([2, 3, 4]) [Link] asserts require('chai').assert
[Link]ould.h[Link]ys(['foo']) assert(val)
Sinon-chai
[Link]ould.h[Link]y('foo') [Link]il(actual, expected)
expect(spy).called
[Link]oul[Link] [Link](val) // is truthy
expect(spy).calledOnce
require('chai').should(); [Link]ual(actual, expected) // 'compare with
expect(spy).calledTwice
//actually call the function, add =='
expect(spy).calledThrice
"should" method to prototype of [Link]rictEqual
object expect(spy).calledBefore(spy2)
[Link]epEqual
expect(spy).calledAfter(spy2)
[Link]
[Link] expect expect(spy).calledWithNew
[Link]False
expect(object).equal(expected) [Link] expect(spy).alwaysCalledWithNew
expect(object).eql(expected) expect(spy).calledOn(context)
[Link]NotNull
expect(object).deep.equal(expected) // same as [Link]Undefined expect(spy).alwaysCalledOn(context)
.eql expect(spy).calledWith(...args)
[Link]Defined
expect(object).be.a('string')
expect(spy).alwaysCalledWith(...args)
[Link]Function
expect(object).include(val)
[Link]Object expect(spy).calledWithExactly(...args)
expect(object).be.ok(val)
expect(object).b[Link]
By Andrey (apk) Published 21st November, 2015. Sponsored by [Link]
[Link]/apk/ Last updated 17th December, 2015. Measure your website readability!
[Link]/ Page 1 of 3. [Link]
JavaScript unit testing tools Cheat Sheet
by Andrey (apk) via [Link]/6656/cs/6151/
Sinon-chai (cont) Sinon-chai (cont) [Link] Spy/stub properties
expect(spy).alwaysCalledWithExactly(...args) [Link]ould.h[Link]ways.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 [Link]ould.h[Link]t.b[Link]lled. .calledOnce
tringOrNothing)
.calledTwice
[Link]ould.h[Link]en.called Mocha BDD
.calledThrice
[Link]ould.h[Link]en.calledOnce [Link]('bdd'); .getCalls() //=> Array
[Link]ould.h[Link]en.calledTwice describe.only('something', .getCall(0)
[Link]ould.h[Link]en.calledThrice function() { .firstCall
spy1.should.have.b[Link]lledBefore(spy2) beforeEach(function() {
}); [Link] Sandbox
spy1.should.have.b[Link]lledAfter(spy2)
it.skip('should work',
[Link]ould.h[Link]en.calledWithNew beforeEach(function() {
function() {
glo[Link] =
[Link]ould.always.have.b[Link]lledWithNew
});
require('sinon').sand[Link]eate();
[Link]ould.h[Link]en.calledOn(context) it('should save',
});
[Link]ould.always.have.b[Link]lledOn(context) function(done) {
afterEach(function() {
[Link]ould.h[Link]en.calledWith(...args) var user = new User();
glo[Link]v.restore();
[Link]ould.always.have.b[Link]lledWith(...args) user.save(function(err) {
});
if (err) throw err;
[Link]ould.always.have.b[Link]lledWithExactly(..
.args) done();
[Link] Fake Server, XHR and date
});
[Link]ould.always.have.b[Link]lledWithExactly(..
}); $.get('/[Link]', ...);
.args)
[Link]quests[0].respond(
})
[Link]ould.h[Link]en.calledWithMatch(...args)
200,
[Link]ould.always.have.b[Link]lledWithMatch(... { "Content-Type":
Mocha TDD
args)
"application/json" },
[Link]ould.h[Link]turned(returnVal) [Link]('tdd');
JSON.stringify([{ id: 1, text:
suite('something', function() {
[Link]ould.h[Link]ways.returned(returnVal) "Provide examples", done: true }])
setup(function() {
[Link]ould.h[Link]rown(errorObjOrErrorTypeStri );
});
ngOrNothing) [Link]store();
test('should work', function() {
xhr =
});
sinon.useFakeXMLHttpRequest();
teardown(function() {
[Link]store();
});
sinon.useFakeTimers(+new
});
Date(2011,9,1));
By Andrey (apk) Published 21st November, 2015. Sponsored by [Link]
[Link]/apk/ Last updated 17th December, 2015. Measure your website readability!
[Link]/ Page 2 of 3. [Link]
JavaScript unit testing tools Cheat Sheet
by Andrey (apk) via [Link]/6656/cs/6151/
[Link] spies [Link] stubs
fn = [Link](); stub = [Link]().returns(42);
fn(); stub() == 42
[Link]ledOnce == true stub
[Link]lCount == 1 .withArgs(42).returns(1);
.withArgs(43).throws("TypeError")
[Link] Spying/stubbing ;
stub
[Link]($, 'ajax')
.returns(1);
$.ajax();
.throws("TypeError");
$.[Link]lledOnce == true
.returnsArg(0); // Return 1st
sinon.stub($, 'ajax', function () {
argument
... }); // function optional
.callsArg(0);
$.[Link]lledWithMatch({ url: '/x'
});
$.[Link]store();
[Link] 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();
[Link]ck(jQuery).expects("ajax").
atLeast(2).atMost(5);
jQuery.a[Link]rify();
By Andrey (apk) Published 21st November, 2015. Sponsored by [Link]
[Link]/apk/ Last updated 17th December, 2015. Measure your website readability!
[Link]/ Page 3 of 3. [Link]