1. 程式人生 > >Chai.js斷言庫expect常用API

Chai.js斷言庫expect常用API

// equal 相等或不相等

expect(4 + 5).to.be.equal(9);

expect(4 + 5).to.be.not.equal(10);

expect('hello').to.equal('hello');

expect(42).to.equal(42);

expect(1).to.not.equal(true);

expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' });

expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });

// above 斷言目標的值大於某個value,如果前面有length的鏈式標記,則可以用來判斷陣列長度或者字串長度

expect(10).to.be.above(5);

expect('foo').to.have.length.above(2);

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

類似的還有least(value)表示大於等於;below(value)表示小於;most(value)表示小於等於

// 判斷目標是否為布林值true(隱式轉換)

expect('everthing').to.be.ok;

expect(1).to.be.ok;

expect(false).to.not.be.ok;

expect(undefined).to.not.be.ok;

expect(null).to.not.be.ok;

// true/false 斷言目標是否為true或false

expect(true).to.be.true;

expect(1).to.not.be.true;

expect(false).to.be.false;

expect(0).to.not.be.false;

// null/undefined 斷言目標是否為null/undefined

expect(null).to.be.null;

expect(undefined).not.to.be.null;

expect(undefined).to.be.undefined;

expect(null).to.not.be.undefined;

// NaN 斷言目標值不是數值

expect('foo').to.be.NaN;

expect(4).not.to.be.NaN;

// 判斷型別大法(可以實現上面的一些例子):a/an expect('test').to.be.a('string');

expect({ foo: 'bar' }).to.be.an('object');

expect(foo).to.be.an.instanceof(Foo);

expect(null).to.be.a('null');

expect(undefined).to.be.an('undefined');

expect(new Error).to.be.an('error');

expect(new Promise).to.be.a('promise');

// 包含關係:用來斷言字串包含和陣列包含。如果用在鏈式呼叫中,可以用來測試物件是否包含某key 可以混著用。

expect([1,2,3]).to.include(2);

expect('foobar').to.contain('foo');

expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');

// 判斷空值

expect([]).to.be.empty;

expect('').to.be.empty;

expect({}).to.be.empty;

// match

expect('foobar').to.match(/^foo/);

// exist 斷言目標既不是null也不是undefined var foo = 'hi' , bar = null, baz;

expect(foo).to.exist;

expect(bar).to.not.exist;

expect(baz).to.not.exist;

// within斷言目標值在某個區間範圍內,可以與length連用

expect(7).to.be.within(5,10);

expect('foo').to.have.length.within(2,4);

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

// instanceOf 斷言目標是某個構造器產生的事例 var Tea = function (name) { this.name = name; } , Chai = new Tea('chai');

expect(Chai).to.be.an.instanceof(Tea);

expect([ 1, 2, 3 ]).to.be.instanceof(Array);

// property(name, [value]) 斷言目標有以name為key的屬性,並且可以指定value斷言屬性值是嚴格相等的,此[value]引數為可選,如果使用deep鏈式呼叫,可以在name中指定物件或陣列的引用表示方法 // simple referencing var obj = { foo: 'bar' };

expect(obj).to.have.property('foo');

expect(obj).to.have.property('foo', 'bar');// 類似於expect(obj).to.contains.keys('foo')

// deep referencing var deepObj = { green: { tea: 'matcha' }, teas: [ 'chai', 'matcha', { tea: 'konacha' } ] };

expect(deepObj).to.have.deep.property('green.tea', 'matcha');

expect(deepObj).to.have.deep.property('teas[1]', 'matcha');

expect(deepObj).to.have.deep.property('teas[2].tea', 'konacha');

// ownproperty 斷言目標擁有自己的屬性,非原型鏈繼承 expect('test').to.have.ownProperty('length');

// throw 斷言目標丟擲特定的異常 var err = new ReferenceError('This is a bad function.'); var fn = function () { throw err; }

expect(fn).to.throw(ReferenceError);

expect(fn).to.throw(Error);

expect(fn).to.throw(/bad function/);

expect(fn).to.not.throw('good function');

expect(fn).to.throw(ReferenceError, /bad function/);

expect(fn).to.throw(err);

expect(fn).to.not.throw(new RangeError('Out of range.'));

// satisfy(method) 斷言目標通過一個真值測試 expect(1).to.satisfy(function(num) { return num > 0; })

更多詳細API請見:https://jestjs.io/docs/en/expect.html