17,292 views 4,406 downloads 21 Nov 2015 History
4.9 avg · 1 rating

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­('st­rin­g')
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(­['fo­o'])
object.sh­oul­d.h­ave.ke­y('f­oo')
object.sh­oul­d.exist
requir­e('c­hai­'­).s­hou­ld();
//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­('s­tri­ng')
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­('ba­r')
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­(['f­oo'])
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('c­hai­'­).e­xpect;

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/, '­reg­exp­') // 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.gr­een­')
assert.pr­ope­rty­Val­(pe­rson, '­nam­e', '­Joh­n')
assert.de­epP­rop­ert­yVa­l(post, '­aut­hor.na­me', '­Joh­n')
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('c­hai­'­).a­ssert

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('s­ino­n');
requir­e('c­hai­'­).u­se(­req­uir­e('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);

Badges

Nice Cheat Sheet Good Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.