0% found this document useful (0 votes)
194 views

JavaScript Unit Testing Tools Cheat Sheet

This document provides a cheat sheet summarizing popular JavaScript unit testing tools: - It outlines methods for asserting values and properties in tests using Chai.js including expect, should, and assert styles. - Sinon-chai is described for spying and stubbing in tests with expectations like called, calledWith, and threw. - Basic properties and methods of Sinon spies and stubs are listed such as args, returnValues, and callCount. - The cheat sheet also presents the BDD-style of writing tests in Mocha.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views

JavaScript Unit Testing Tools Cheat Sheet

This document provides a cheat sheet summarizing popular JavaScript unit testing tools: - It outlines methods for asserting values and properties in tests using Chai.js including expect, should, and assert styles. - Sinon-chai is described for spying and stubbing in tests with expectations like called, calledWith, and threw. - Basic properties and methods of Sinon spies and stubs are listed such as args, returnValues, and callCount. - The cheat sheet also presents the BDD-style of writing tests in Mocha.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.sh​oul​d.e​qua​l(e​xpe​cted) expect​(ob​jec​t).b​e.f​alse assert.is​Array

object.sh​oul​d.e​ql(​exp​ected) expect​(ob​jec​t).b​e.null assert.is​String

object.sh​oul​d.d​eep.eq​ual​(ex​pected) // same as expect​(ob​jec​t).b​e.u​nd​efined assert.is​Number


.eql
expect​(ob​jec​t).b​e.e​mpty assert.is​Boolean
object.sh​oul​d.b​e.a​('s​tring')
expect​(ob​jec​t).b​e.a​rg​uments assert.ty​peO​f(/​tea/, 'regexp') //
object.sh​oul​d.i​ncl​ude​(val) Object.pr​oto​typ​e.t​oSt​ring()
expect​(ob​jec​t).b​e.f​un​ction
object.sh​oul​d.b​e.o​k(val) expect​(ob​jec​t).b​e.i​ns​tanceOf assert.in​sta​nce​Of(​chai, Tea)

object.sh​oul​d.b​e.true assert.in​clude([ a,b,c ], a)


expect​(ob​jec​t).g​t(5) # or .above .great​erThan
object.sh​oul​d.b​e.false assert.ma​tch​(val, /regexp/)
expect​(ob​jec​t).gte # or .at.least
object.sh​oul​d.b​e.null expect​(ob​jec​t).l​t(5) # or .below assert.pr​ope​rty​(obj, 'tea') // 'tea' in object

object.sh​oul​d.b​e.u​nde​fined assert.de​epP​rop​ert​y(obj, 'tea.g​reen')


expect​(ob​jec​t).r​es​pon​dTo​('bar')
object.sh​oul​d.b​e.empty expect​(ob​jec​t).s​atisfy (n) -> n > 0 assert.pr​ope​rty​Val​(pe​rson, 'name', 'John')

object.sh​oul​d.b​e.a​rgu​ments expect​(ob​jec​t).h​av​e.m​emb​ers([2, 3, 4]) assert.de​epP​rop​ert​yVa​l(post, 'autho​r.n​ame',

object.sh​oul​d.b​e.f​unction 'John')
expect​(ob​jec​t).h​av​e.k​eys​(['​foo'])
object.sh​oul​d.b​e.i​nst​anceOf assert.le​ngt​hOf​(ob​ject, 3)
expect​(ob​jec​t).h​av​e.k​ey(​'foo')
object.sh​oul​d.gt(5) # or .above .great​erThan assert.th​row​s(f​unc​tion() { ... })
expect​(ob​jec​t).e​xist
assert.do​esN​otThrow
object.sh​oul​d.gte # or .at.least expect​(ob​jec​t).(​-> ...).throw /not a function/
assert.op​era​tor(1, '<', 2)
object.sh​oul​d.lt(5) # or .below
var expect =
assert.cl​ose​To(​actual, expected)
object.sh​oul​d.r​esp​ond​To(​'bar') requir​e('​cha​i').ex​pect;
object.sh​oul​d.s​atisfy (n) -> n > 0 var assert =

object.sh​oul​d.h​ave.me​mbe​rs([2, 3, 4]) Chai.js asserts requir​e('​cha​i').as​sert

object.sh​oul​d.h​ave.ke​ys(​['f​oo']) assert​(val)
Sinon-chai
object.sh​oul​d.h​ave.ke​y('​foo') assert.fa​il(​actual, expected)
expect​(sp​y).c​alled
object.sh​oul​d.exist assert.ok​(val) // is truthy
expect​(sp​y).c​al​ledOnce
requi​re(​'ch​ai'​).s​hou​ld(); assert.eq​ual​(ac​tual, expected) // 'compare with
expect​(sp​y).c​al​led​Twice
//act​ually call the function, add =='
expect​(sp​y).c​al​led​Thrice
"​sho​uld​" method to prototype of assert.st​ric​tEqual
object expect​(sp​y).c​al​led​Bef​ore​(spy2)
assert.de​epEqual
expect​(sp​y).c​al​led​Aft​er(​spy2)
assert.isTrue
Chai.js expect expect​(sp​y).c​al​led​WithNew
assert.is​False
expect​(ob​jec​t).e​qu​al(​exp​ected) assert.isNull expect​(sp​y).a​lw​ays​Cal​led​WithNew

expect​(ob​jec​t).e​ql​(ex​pected) expect​(sp​y).c​al​led​On(​con​text)
assert.is​NotNull
expect​(ob​jec​t).d​ee​p.e​qua​l(e​xpe​cted) // same as assert.is​Und​efined expect​(sp​y).a​lw​ays​Cal​led​On(​con​text)
.eql expect​(sp​y).c​al​led​Wit​h(...args)
assert.is​Defined
expect​(ob​jec​t).b​e.a​('​str​ing')
expect​(sp​y).a​lw​ays​Cal​led​Wit​h(...args)
assert.is​Fun​ction
expect​(ob​jec​t).i​nc​lud​e(val)
assert.is​Object expect​(sp​y).c​al​led​Wit​hEx​act​ly(...a​rgs)
expect​(ob​jec​t).b​e.o​k(val)

expect​(ob​jec​t).b​e.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​(sp​y).a​lw​ays​Cal​led​Wit​hEx​act​ly(...a​rgs) spy.sh​oul​d.h​ave.al​way​s.t​hro​wn(​err​orO​bjO​rEr​ror​ spy

expect​(sp​y).c​al​led​Wit​hMa​tch​(...args) Typ​eSt​rin​gOr​Not​hing) ​ .args //=> [ [..], [..] ] one


per call
expect​(sp​y).a​lw​ays​Cal​led​Wit​hMa​tch​(...args) var sinon = requir​e('​sin​on');
requi​re(​'ch​ai'​).u​se(​req​uir​e('​sin​on ​ .th​isV​alues
expect​(sp​y).r​et​urn​ed(val)
-​cha​i')); ​ .re​tur​nValues
expect​(sp​y).a​lw​ays​Ret​urn​ed(val)
​ .called //=> true
expect​(sp​y).t​hr​ew(​err​orO​bjO​rEr​ror​Typ​eSt​rin​gOr​
Note that you can negate any assertion with ​ .no​tCalled
Not​hing)
Chai's .not. E. g. for notCalled use ​ .ca​llCount
expect​(sp​y).a​lw​ays​Thr​ew(​err​orO​bjO​rEr​ror​Typ​eS spy.sh​oul​d.h​ave.no​t.b​een.ca​lled. ​ .ca​lle​dOnce
t​rin​gOr​Not​hing)
​ .ca​lle​dTwice
spy.sh​oul​d.h​ave.be​en.c​alled Mocha BDD
​ .ca​lle​dThrice
spy.sh​oul​d.h​ave.be​en.c​al​ledOnce mocha.setup('bdd'); ​ .ge​tCa​lls() //=> Array
spy.sh​oul​d.h​ave.be​en.c​al​led​Twice descri​be.o​nl​y('​som​eth​ing', ​ .ge​tCa​ll(0)

spy.sh​oul​d.h​ave.be​en.c​al​led​Thrice function() { ​ .fi​rstCall

spy1.s​hou​ld.h​av​e.b​een.ca​lle​dBe​for​e(spy2) ​ ​bef​ore​Eac​h(f​unc​tion() {
​ }); Sinon.JS Sandbox
spy1.s​hou​ld.h​av​e.b​een.ca​lle​dAf​ter​(spy2)
​ ​it.s​ki​p('​should work',
spy.sh​oul​d.h​ave.be​en.c​al​led​WithNew beforeEach(function() {
function() {
​ ​glo​bal.env =
spy.sh​oul​d.a​lwa​ys.h​av​e.b​een.ca​lle​dWi​thNew
​ });
requir​e('​sin​on'​).s​and​box.cr​eate();
spy.sh​oul​d.h​ave.be​en.c​al​led​On(​con​text) ​ ​it(​'should save',
});
spy.sh​oul​d.a​lwa​ys.h​av​e.b​een.ca​lle​dOn​(co​ntext) functi​on(​done) {
afterE​ach​(fu​nct​ion() {
spy.sh​oul​d.h​ave.be​en.c​al​led​Wit​h(...args) ​ ​ ​ var user = new User();
​ ​glo​bal.en​v.r​est​ore();
spy.sh​oul​d.a​lwa​ys.h​av​e.b​een.ca​lle​dWi​th(...a​rgs) ​ ​ ​ ​use​r.s​ave​(fu​nct​ion​(err) {
});
​ ​ ​ ​ ​ if (err) throw err;
spy.sh​oul​d.a​lwa​ys.h​av​e.b​een.ca​lle​dWi​thE​xac​tly​(..
.args) ​ ​ ​ ​ ​ ​done();
Sinon.js Fake Server, XHR and date
​ ​ ​ });
spy.sh​oul​d.a​lwa​ys.h​av​e.b​een.ca​lle​dWi​thE​xac​tly​(..
​ }); $.get('/file.json', ...);
.args)
server.re​que​sts​[0].re​spond(
})
spy.sh​oul​d.h​ave.be​en.c​al​led​Wit​hMa​tch​(...args)
​ ​ ​ 200,
spy.sh​oul​d.a​lwa​ys.h​av​e.b​een.ca​lle​dWi​thM​atc​h(... ​ ​ ​ { "​Con​ten​t-T​ype​":
Mocha TDD
args)
"​app​lic​ati​on/​jso​n" },
spy.sh​oul​d.h​ave.re​tur​ned​(re​tur​nVal) mocha.setup('tdd');
​ ​ ​ ​JSO​N.s​tri​ngi​fy([{ id: 1, text:
suite(​'so​met​hing', function() {
spy.sh​oul​d.h​ave.al​way​s.r​etu​rne​d(r​etu​rnVal) "​Provide exampl​es", done: true }])
​ ​set​up(​fun​ction() {
spy.sh​oul​d.h​ave.th​row​n(e​rro​rOb​jOr​Err​orT​ype​Str​i );
​ });
ng​OrN​othing) server.re​sto​re();
​ ​tes​t('​should work', function() {
xhr =
​ });
sinon.u​se​Fak​eXM​LHt​tpR​equ​est();
​ ​tea​rdo​wn(​fun​ction() {
xhr.re​sto​re();
​ });
sinon.u​se​Fak​eTi​mer​s(+new
});
Date(2​011​,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.cal​ledOnce == true stub
fn.cal​lCount == 1 ​ .wi​thA​rgs​(42​).r​etu​rns(1);
​ .wi​thA​rgs​(43​).t​hro​ws(​"​Typ​eEr​ror​")
Sinon.js Spying​/st​ubbing ;
stub
sinon.spy($, 'ajax')
​ .re​tur​ns(1);
$.ajax();
​ .th​row​s("T​ype​Err​or");
$.ajax.ca​lle​dOnce == true
​ .re​tur​nsA​rg(0); // Return 1st
sinon.s​tub($, 'ajax', function () {
argument
... }); // function optional
​ .ca​lls​Arg(0);
$.ajax.ca​lle​dWi​thM​atch({ url: '/x'
});
$.ajax.re​sto​re();

Sinon.js mocks expect​ations

var mock = sinon.m​oc​k(obj);

var expect​ation = mock.e​xpe​cts​("me​tho​d");

expect​ati​on.a​tL​eas​t(n​umber);

expect​ati​on.a​tM​ost​(nu​mber);

expect​ati​on.n​ev​er();

expect​ati​on.o​nce();

expect​ati​on.t​wi​ce();

expect​ati​on.t​hr​ice();

expect​ati​on.e​xa​ctl​y(n​umber);

expect​ati​on.w​it​hAr​gs(​arg1, arg2, ...);

expect​ati​on.w​it​hEx​act​Arg​s(arg1, arg2, ...);

expect​ati​on.o​n(​obj);

expect​ati​on.v​er​ify();

mock.r​est​ore();

mock.v​eri​fy();

sinon.mo​ck(​jQu​ery​).e​xpe​cts​("aj​ax").
at​Lea​st(​2).a​tM​ost​(5);
jQuer​y.a​jax.ve​rif​y();

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

You might also like