Show Menu
Cheatography

JavaScript unit testing tools Cheat Sheet by

Chai.js should

object.sh­oul­d.e­qua­l(e­xpe­cted)
object.sh­oul­d.e­ql(­exp­ected)
object.sh­oul­d.d­eep.eq­ual­(ex­pected) // same as .eql
object.sh­oul­d.b­e.a­('s­tring')
object.sh­oul­d.i­ncl­ude­(val)
object.sh­oul­d.b­e.o­k(val)
object.sh­oul­d.b­e.true
object.sh­oul­d.b­e.false
object.sh­oul­d.b­e.null
object.sh­oul­d.b­e.u­nde­fined
object.sh­oul­d.b­e.empty
object.sh­oul­d.b­e.a­rgu­ments
object.sh­oul­d.b­e.f­unction
object.sh­oul­d.b­e.i­nst­anceOf
object.sh­oul­d.gt(5) # or .above .great­erThan
object.sh­oul­d.gte # or .at.least
object.sh­oul­d.lt(5) # or .below
object.sh­oul­d.r­esp­ond­To(­'bar')
object.sh­oul­d.s­atisfy (n) -> n > 0
object.sh­oul­d.h­ave.me­mbe­rs([2, 3, 4])
object.sh­oul­d.h­ave.ke­ys(­['f­oo'])
object.sh­oul­d.h­ave.ke­y('­foo')
object.sh­oul­d.exist
requir­e('­cha­i').sh­ould();

//actually call the function, add "­sho­uld­" method to prototype of object

Chai.js expect

expect­(ob­jec­t).e­qu­al(­exp­ected)
expect­(ob­jec­t).e­ql­(ex­pected)
expect­(ob­jec­t).d­ee­p.e­qua­l(e­xpe­cted) // same as .eql
expect­(ob­jec­t).b­e.a­('­str­ing')
expect­(ob­jec­t).i­nc­lud­e(val)
expect­(ob­jec­t).b­e.o­k(val)
expect­(ob­jec­t).b­e.true
expect­(ob­jec­t).b­e.f­alse
expect­(ob­jec­t).b­e.null
expect­(ob­jec­t).b­e.u­nd­efined
expect­(ob­jec­t).b­e.e­mpty
expect­(ob­jec­t).b­e.a­rg­uments
expect­(ob­jec­t).b­e.f­un­ction
expect­(ob­jec­t).b­e.i­ns­tanceOf
expect­(ob­jec­t).g­t(5) # or .above .great­erThan
expect­(ob­jec­t).gte # or .at.least
expect­(ob­jec­t).l­t(5) # or .below
expect­(ob­jec­t).r­es­pon­dTo­('bar')
expect­(ob­jec­t).s­atisfy (n) -> n > 0
expect­(ob­jec­t).h­av­e.m­emb­ers([2, 3, 4])
expect­(ob­jec­t).h­av­e.k­eys­(['­foo'])
expect­(ob­jec­t).h­av­e.k­ey(­'foo')
expect­(ob­jec­t).e­xist
expect­(ob­jec­t).(­-> ...).throw /not a function/
var expect = requir­e('­cha­i').ex­pect;

Chai.js asserts

assert­(val)
assert.fa­il(­actual, expected)
assert.ok­(val) // is truthy
assert.eq­ual­(ac­tual, expected) // 'compare with =='
assert.st­ric­tEqual
assert.de­epEqual
assert.isTrue
assert.is­False
assert.isNull
assert.is­NotNull
assert.is­Und­efined
assert.is­Defined
assert.is­Fun­ction
assert.is­Object
assert.is­Array
assert.is­String
assert.is­Number
assert.is­Boolean
assert.ty­peO­f(/­tea/, 'regexp') // Object.pr­oto­typ­e.t­oSt­ring()
assert.in­sta­nce­Of(­chai, Tea)
assert.in­clude([ a,b,c ], a)
assert.ma­tch­(val, /regexp/)
assert.pr­ope­rty­(obj, 'tea') // 'tea' in object
assert.de­epP­rop­ert­y(obj, 'tea.g­reen')
assert.pr­ope­rty­Val­(pe­rson, 'name', 'John')
assert.de­epP­rop­ert­yVa­l(post, 'autho­r.n­ame', 'John')
assert.le­ngt­hOf­(ob­ject, 3)
assert.th­row­s(f­unc­tion() { ... })
assert.do­esN­otThrow
assert.op­era­tor(1, '<', 2)
assert.cl­ose­To(­actual, expected)
var assert = requir­e('­cha­i').assert
 

Sinon-chai

expect­(sp­y).c­alled
expect­(sp­y).c­al­ledOnce
expect­(sp­y).c­al­led­Twice
expect­(sp­y).c­al­led­Thrice
expect­(sp­y).c­al­led­Bef­ore­(spy2)
expect­(sp­y).c­al­led­Aft­er(­spy2)
expect­(sp­y).c­al­led­WithNew
expect­(sp­y).a­lw­ays­Cal­led­WithNew
expect­(sp­y).c­al­led­On(­con­text)
expect­(sp­y).a­lw­ays­Cal­led­On(­con­text)
expect­(sp­y).c­al­led­Wit­h(...args)
expect­(sp­y).a­lw­ays­Cal­led­Wit­h(...args)
expect­(sp­y).c­al­led­Wit­hEx­act­ly(...a­rgs)
expect­(sp­y).a­lw­ays­Cal­led­Wit­hEx­act­ly(...a­rgs)
expect­(sp­y).c­al­led­Wit­hMa­tch­(...args)
expect­(sp­y).a­lw­ays­Cal­led­Wit­hMa­tch­(...args)
expect­(sp­y).r­et­urn­ed(val)
expect­(sp­y).a­lw­ays­Ret­urn­ed(val)
expect­(sp­y).t­hr­ew(­err­orO­bjO­rEr­ror­Typ­eSt­rin­gOr­Not­hing)
expect­(sp­y).a­lw­ays­Thr­ew(­err­orO­bjO­rEr­ror­Typ­eSt­rin­gOr­Not­hing)
spy.sh­oul­d.h­ave.be­en.c­alled
spy.sh­oul­d.h­ave.be­en.c­al­ledOnce
spy.sh­oul­d.h­ave.be­en.c­al­led­Twice
spy.sh­oul­d.h­ave.be­en.c­al­led­Thrice
spy1.s­hou­ld.h­av­e.b­een.ca­lle­dBe­for­e(spy2)
spy1.s­hou­ld.h­av­e.b­een.ca­lle­dAf­ter­(spy2)
spy.sh­oul­d.h­ave.be­en.c­al­led­WithNew
spy.sh­oul­d.a­lwa­ys.h­av­e.b­een.ca­lle­dWi­thNew
spy.sh­oul­d.h­ave.be­en.c­al­led­On(­con­text)
spy.sh­oul­d.a­lwa­ys.h­av­e.b­een.ca­lle­dOn­(co­ntext)
spy.sh­oul­d.h­ave.be­en.c­al­led­Wit­h(...args)
spy.sh­oul­d.a­lwa­ys.h­av­e.b­een.ca­lle­dWi­th(...a­rgs)
spy.sh­oul­d.a­lwa­ys.h­av­e.b­een.ca­lle­dWi­thE­xac­tly­(...args)
spy.sh­oul­d.a­lwa­ys.h­av­e.b­een.ca­lle­dWi­thE­xac­tly­(...args)
spy.sh­oul­d.h­ave.be­en.c­al­led­Wit­hMa­tch­(...args)
spy.sh­oul­d.a­lwa­ys.h­av­e.b­een.ca­lle­dWi­thM­atc­h(...args)
spy.sh­oul­d.h­ave.re­tur­ned­(re­tur­nVal)
spy.sh­oul­d.h­ave.al­way­s.r­etu­rne­d(r­etu­rnVal)
spy.sh­oul­d.h­ave.th­row­n(e­rro­rOb­jOr­Err­orT­ype­Str­ing­OrN­othing)
spy.sh­oul­d.h­ave.al­way­s.t­hro­wn(­err­orO­bjO­rEr­ror­Typ­eSt­rin­gOr­Not­hing)
var sinon = requir­e('­sin­on');

requir­e('­cha­i').us­e(r­equ­ire­('s­ino­n-c­hai'));


Note that you can negate any assertion with Chai's .not. E. g. for notCalled use spy.sh­oul­d.h­ave.no­t.b­een.ca­lled.

Mocha BDD

mocha.setup('bdd');

describe.only('something', function() {
  beforeEach(function() {
  });

  it.skip('should work', function() {
  });

  it('should save', function(done) {
    var user = new User();
    user.save(function(err) {
      if (err) throw err;
      done();
    });
  });
})

Mocha TDD

mocha.setup('tdd');

suite('something', function() {
  setup(function() {
  });

  test('should work', function() {
  });

  teardown(function() {
  });
});
 

Sinon.js Spy/stub properties

spy
  .args        //=> [ [..], [..] ] one per call
  .thisValues
  .returnValues

  .called      //=> true
  .notCalled
  .callCount
  .calledOnce
  .calledTwice
  .calledThrice

  .getCalls()   //=> Array
  .getCall(0)
  .firstCall

Sinon.JS Sandbox

beforeEach(function() {
  global.env = require('sinon').sandbox.create();
});

afterEach(function() {
  global.env.restore();
});

Sinon.js Fake Server, XHR and date

$.get('/file.json', ...);
server.requests[0].respond(
    200,
    { "Content-Type": "application/json" },
    JSON.stringify([{ id: 1, text: "Provide examples", done: true }])
);

server.restore();

xhr = sinon.useFakeXMLHttpRequest();
xhr.restore();

sinon.useFakeTimers(+new Date(2011,9,1));

Sinon.js spies

fn = sinon.spy();

fn();
fn.calledOnce == true
fn.callCount == 1

Sinon.js Spying­/st­ubbing

sinon.spy($, 'ajax')

$.ajax();
$.ajax.calledOnce == true

sinon.stub($, 'ajax', function () { ... }); // function optional

$.ajax.calledWithMatch({ url: '/x' });
$.ajax.restore();

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.m­oc­k(j­Que­ry).ex­pec­ts(­"­aja­x").a­tL­eas­t(2­).a­tMo­st(5);

jQuery.aj­ax.v­er­ify();

Sinon.js stubs

stub = sinon.stub().returns(42);
stub() == 42

stub
  .withArgs(42).returns(1);
  .withArgs(43).throws("TypeError");

stub
  .returns(1);
  .throws("TypeError");
  .returnsArg(0); // Return 1st argument
  .callsArg(0);
                                       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by apk

          CoffeeScript language Cheat Sheet