should.jsをベースに開発されたBDDアサーションライブラリ。
-
IE6+, Firefox, Chrome, Safari, Operaで動く
-
全てのテストフレームワークで使用可能
-
NodeでもOK
-
スタンドアローン
$ npm install -g expect.js
でインストールして、対象ファイルで
var expect = require('expect.js');
<script src="expect.js"></script>
expect(1).to.be.ok();
expect(true).to.be.ok();
expect({}).to.be.ok();
expect(0).to.not.be.ok();
expect(1).to.be(1)
expect(NaN).not.to.equal(NaN);
expect(1).not.to.be(true)
expect('1').to.not.be(1);
expect({ a: 'b' }).to.eql({ a: 'b' });
expect(1).to.eql('1');
// typeof(arrayにも対応)
expect(5).to.be.a('number');
expect([]).to.be.an('array'); // arrayでもtrue
expect([]).to.be.an('object'); // typeofなのでこれもtrue
// コンストラクタ
expect(5).to.be.a(Number);
expect([]).to.be.an(Array);
expect(tobi).to.be.a(Ferret);
expect(person).to.be.a(Mammal);
expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
expect([1, 2]).to.contain(1);
expect('hello world').to.contain('world');
expect([]).to.have.length(0); expect([1,2,3]).to.have.length(3);
expect([]).to.be.empty();
expect({}).to.be.empty();
expect({ length: 0, duck: 'typing' }).to.be.empty();
expect({ my: 'object' }).to.not.be.empty();
expect([1,2,3]).to.not.be.empty();
expect(window).to.have.property('expect')
expect(window).to.have.property('expect', expect)
expect({a: 'b'}).to.have.property('a');
expect({ a: 'b' }).to.have.key('a');
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c');
expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']);
expect({ a: 'b', c: 'd' }).to.not.only.have.key('a');
expect(fn).to.throwError(); // 例外処理と同義
expect(fn).to.throwException(function (e) { // 例外オブジェクトを引数でとる
expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn2).to.not.throwException();
expect(1).to.be.within(0, Infinity);
expect(3).to.be.above(0); expect(5).to.be.greaterThan(3);
expect(0).to.be.below(3); expect(1).to.be.lessThan(3);
expect().fail()
expect().fail("Custom failure message")