0% found this document useful (0 votes)
87 views3 pages

JavaScript Unit Testing 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)
87 views3 pages

JavaScript Unit Testing 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

JavaScript unit testing tools Cheat Sheet

by Andrey (apk) via [Link]/6656/cs/6151/

[Link] should [Link] expect (cont) [Link] asserts (cont)

[Link]​oul​d.e​qua​l(e​xpe​cted) expect​(ob​jec​t).b​e.f​alse [Link]​Array

[Link]​oul​d.e​ql(​exp​ected) expect​(ob​jec​t).b​[Link] [Link]​String

[Link]​oul​d.d​[Link]​ual​(ex​pected) // same as expect​(ob​jec​t).b​e.u​nd​efined [Link]​Number


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

[Link]​oul​d.b​[Link] [Link]​clude([ a,b,c ], a)


expect​(ob​jec​t).g​t(5) # or .above .great​erThan
[Link]​oul​d.b​[Link] [Link]​tch​(val, /regexp/)
expect​(ob​jec​t).gte # or .[Link]
[Link]​oul​d.b​[Link] expect​(ob​jec​t).l​t(5) # or .below [Link]​ope​rty​(obj, 'tea') // 'tea' in object

[Link]​oul​d.b​e.u​nde​fined [Link]​epP​rop​ert​y(obj, 'tea.g​reen')


expect​(ob​jec​t).r​es​pon​dTo​('bar')
[Link]​oul​d.b​[Link] expect​(ob​jec​t).s​atisfy (n) -> n > 0 [Link]​ope​rty​Val​(pe​rson, 'name', 'John')

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

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

[Link]​oul​d.h​[Link]​mbe​rs([2, 3, 4]) [Link] asserts requir​e('​cha​i').as​sert

[Link]​oul​d.h​[Link]​ys(​['f​oo']) assert​(val)
Sinon-chai
[Link]​oul​d.h​[Link]​y('​foo') [Link]​il(​actual, expected)
expect​(sp​y).c​alled
[Link]​oul​[Link] [Link]​(val) // is truthy
expect​(sp​y).c​al​ledOnce
requi​re(​'ch​ai'​).s​hou​ld(); [Link]​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 [Link]​ric​tEqual
object expect​(sp​y).c​al​led​Bef​ore​(spy2)
[Link]​epEqual
expect​(sp​y).c​al​led​Aft​er(​spy2)
[Link]
[Link] expect expect​(sp​y).c​al​led​WithNew
[Link]​False
expect​(ob​jec​t).e​qu​al(​exp​ected) [Link] 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)
[Link]​NotNull
expect​(ob​jec​t).d​ee​p.e​qua​l(e​xpe​cted) // same as [Link]​Und​efined expect​(sp​y).a​lw​ays​Cal​led​On(​con​text)
.eql expect​(sp​y).c​al​led​Wit​h(...args)
[Link]​Defined
expect​(ob​jec​t).b​e.a​('​str​ing')
expect​(sp​y).a​lw​ays​Cal​led​Wit​h(...args)
[Link]​Fun​ction
expect​(ob​jec​t).i​nc​lud​e(val)
[Link]​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​[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​(sp​y).a​lw​ays​Cal​led​Wit​hEx​act​ly(...a​rgs) [Link]​oul​d.h​[Link]​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 [Link]​oul​d.h​[Link]​t.b​[Link]​lled. ​ .ca​lle​dOnce
t​rin​gOr​Not​hing)
​ .ca​lle​dTwice
[Link]​oul​d.h​[Link]​en.c​alled Mocha BDD
​ .ca​lle​dThrice
[Link]​oul​d.h​[Link]​en.c​al​ledOnce [Link]('bdd'); ​ .ge​tCa​lls() //=> Array
[Link]​oul​d.h​[Link]​en.c​al​led​Twice descri​be.o​nl​y('​som​eth​ing', ​ .ge​tCa​ll(0)

[Link]​oul​d.h​[Link]​en.c​al​led​Thrice function() { ​ .fi​rstCall

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

[Link] 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();

[Link]​ck(​jQu​ery​).e​xpe​cts​("aj​ax").
at​Lea​st(​2).a​tM​ost​(5);
jQuer​y.a​[Link]​rif​y();

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]

You might also like