LoginSignup
30
29

More than 5 years have passed since last update.

Expect.jsのREADMEを和訳してみた。

Posted at

Expect.js

should.jsをべースに開発されたミニマムなBDDアサーションライブラリ。

expect(window.r).to.be(undefined);
expect({ a: 'b' }).to.eql({ a: 'b' })
expect(5).to.be.a('number');
expect([]).to.be.an('array');
expect(window).not.to.be.an(Image);

特徴

  • クロスブラウザ: IE6+, Firefox, Safari, Chrome, Operaで動作する。
  • 全てのテスティングフレームワークと併用可能。
  • Node.JSで使用可能(require('expect.js')).
  • スタンドアローン

使い方

Node

npmを利用してインストール。

$ npm install expect.js
var expect = require('expect.js');

ブラウザ

<script src="expect.js"></script>

API

ok: 値が truthy か否か検証。

expect(1).to.be.ok();
expect(true).to.be.ok();
expect({}).to.be.ok();
expect(0).to.not.be.ok();

be / equal: 厳密等価演算子を用いて値を検証。

expect(1).to.be(1)
expect(NaN).not.to.equal(NaN);
expect(1).not.to.be(true)
expect('1').to.not.be(1);

eql: 等価演算子で等価な値か検証(objectも含む)。

expect({ a: 'b' }).to.eql({ a: 'b' });
expect(1).to.eql('1');

a/an: typeof演算子を用いて指定したデータ型の値か検証。array、instanceof演算子に対応。

// typeof with optional `array`
expect(5).to.be.a('number');
expect([]).to.be.an('array');  // works
expect([]).to.be.an('object'); // works too, since it uses `typeof`

// constructors
expect(5).to.be.a(Number);
expect([]).to.be.an(Array);
expect(tobi).to.be.a(Ferret);
expect(person).to.be.a(Mammal);

match: 文字列が正規表現に一致するか検証。

expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);

contain: 文字列・配列の中に値が含まれているか検証。

expect([1, 2]).to.contain(1);
expect('hello world').to.contain('world');

length: 配列のlengthプロパティを検証。

expect([]).to.have.length(0);
expect([1,2,3]).to.have.length(3);

empty: 配列が空か否かを検証。

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();

property: プロパティの存在を検証(値もオプションで指定可能)。

expect(window).to.have.property('expect')
expect(window).to.have.property('expect', expect)
expect({a: 'b'}).to.have.property('a');

key/keys: オブジェクトのキーの存在を検証。

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');

throwException/throwError: 関数が呼ばれた際に例外を投げるか否かを検証。

expect(fn).to.throwError(); // synonym of throwException
expect(fn).to.throwException(function (e) { // get the exception object
  expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn2).to.not.throwException();

within: 数値が範囲内かを検証。

expect(1).to.be.within(0, Infinity);

greaterThan/above: 「より大きいか」を検証。

expect(3).to.be.above(0);
expect(5).to.be.greaterThan(3);

lessThan/below: 「より小さいか」を検証。

expect(0).to.be.below(3);
expect(1).to.be.lessThan(3);

fail: 明示的にテストを失敗させる。

expect().fail()
expect().fail("Custom failure message")
30
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
29