Snelle referentie

Chai.js Assertiebibliotheek Spiekbrief

Beknopte handleiding voor Chai.js: BDD expect- en should-stijlen, TDD assert-stijl, diepe gelijkheid, typecontrole en chai-jquery-asserties.

· Gepubliceerd

Chai Chai is een BDD / TDD-assertiebibliotheek voor Node.js en de browser die naadloos combineert met Mocha of Jest.

BDD-stijl (expect)

const { expect } = require('chai');

Gelijkheid & types

expect(val).to.equal(expected);         // Strikte gelijkheid (===)
expect(val).to.eql(expected);           // Diepe gelijkheid (Deep equality)
expect(val).to.deep.equal(expected);    // Zelfde als .eql

expect(val).to.be.a('string');
expect(val).to.be.an('array');
expect(val).to.be.an.instanceOf(User);

Waarheid & aanwezigheid

expect(val).to.be.ok;          // Truthy-waarde
expect(val).to.be.true;
expect(val).to.be.false;
expect(val).to.exist;          // Niet null en niet undefined
expect(val).to.be.null;
expect(val).to.be.undefined;
expect(val).to.be.empty;        // Lege array, string of object

Numerieke vergelijkingen

expect(score).to.be.above(5);           // Groter dan (gt)
expect(score).to.be.at.least(5);        // Groter dan of gelijk aan (gte)
expect(score).to.be.below(10);          // Kleiner dan (lt)
expect(score).to.be.at.most(10);        // Kleiner dan of gelijk aan (lte)
expect(score).to.be.within(5, 10);      // Inclusief bereik
expect(pi).to.be.closeTo(3.14, 0.01);   // Drijvende-kommatolerantie

Objecten & collecties

expect(array).to.include(item);
expect(array).to.have.members([1, 2, 3]);
expect(array).to.have.lengthOf(3);

expect(obj).to.have.property('name');
expect(obj).to.have.property('name', 'Alice');
expect(obj).to.have.nested.property('address.city', 'London');
expect(obj).to.have.all.keys('id', 'name');

Foutafhandeling van functies

expect(() => badFunction()).to.throw();
expect(() => badFunction()).to.throw(TypeError);
expect(() => badFunction()).to.throw(/invalid argument/);
expect(() => goodFunction()).to.not.throw();

TDD-stijl (assert)

const { assert } = require('chai');

assert.ok(val, 'value is truthy');
assert.equal(actual, expected, 'compares with ==');
assert.strictEqual(actual, expected, 'compares with ===');
assert.deepEqual(actual, expected, 'deep equality');

assert.isTrue(val);
assert.isFalse(val);
assert.isNull(val);
assert.isNotNull(val);
assert.isUndefined(val);
assert.isDefined(val);

assert.typeOf(val, 'string');
assert.instanceOf(val, Model);
assert.lengthOf(val, 3);
assert.include(list, item);
assert.match(str, /regex/);
assert.throws(() => { /* error test */ }, /error regex/);

Chai met jQuery (chai-jquery)

chai.use(require('chai-jquery'));

// DOM-statuscontroles
expect($elem).to.be.visible;
expect($elem).to.be.hidden;
expect($elem).to.be.enabled;
expect($elem).to.be.disabled;
expect($elem).to.be.checked;

// CSS & attributen
expect($elem).to.have.class('active');
expect($elem).to.have.id('submit-btn');
expect($elem).to.have.attr('disabled');
expect($elem).to.have.css('display', 'block');
expect($elem).to.have.text('Hello World');

Verwante onderwerpen