只測試指定測試套件, 同一層級中出現 it
, fit
兩個測試 spec
, 將忽略 it
, 同理,同一層級出現 describe
和fdescribe
,將會忽略desribe
javascript
describe("Focused specs", function() { fit("is focused and will run", function() { expect(true).toBeTruthy(); }); // 忽略該測試 spec it('is not focused and will not run', function(){ expect(true).toBeFalsy(); }); fdescribe('focused describe', function(){ it('will run', function(){ expect(true).toBeTruthy(); }); it('will also run', function(){ expect(true).toBeTruthy(); }); }); fdescribe('another focused describe', function(){ // 忽略該測試 fit("is focused and will run", function() { expect(true).toBeTruthy(); }); it('is not focused and will not run', function(){ expect(true).toBeFalsy(); }); }); // 忽略該測試 describe('ignore describe', function () { fit("is focused and will run", function() { expect(true).toBeTruthy(); }); }) });
自定義比較函數, 覆蓋 toEqual
比較函數. 在beforeEach
聲明週期使用 jasmine.addCustomEqualityTester
, 返回true
或 false
, 若是返回 undefined
或 null
. 則表示使用默認的toEqual
進行比較java
describe('test suite', function () { beforeEach(function() { jasmine.addCustomEqualityTester(function (actual, expected) { var actualType = typeof actual if (actualType === 'string' && actualType === typeof expected) { return actual[0] === expected[0] } }); }); it('should be custom equal', function () { expect('aa').toEqual('ab'); }); });
自定義匹配規則, 在beforeEach
聲明週期使用 jasmine.addMatchers
加入自定義匹配規則. 比 custom equality
更完善, 能夠自定義錯誤信息.segmentfault
beforeEach(function () { jasmine.addMatchers({ // TO BE LIKE toBeLike: function (util, customEqualityTesters) { return { // 匹配規則 // 參數 actual 實際傳入值 // 參數 expected 期待值 compare: function (actual, expected) { var pass = false; var message = ''; var actualType = typeof actual; var expectedType = typeof expected; if (actual === expected) { pass = true; } else if (actual === null) { pass = expected === null || expected === undefined; } else if (actualType === expectedType) { pass = true; if (actualType === 'object') { for (name in actual) { if (actual[name] === null || actual[name] === undefined) { pass = expected === null || expected === undefined; } else if (typeof actual[name] !== typeof expected[name]) { pass = false; } if (!pass) { break; } } } } if (pass) { message = 'Expected ' + actual + ' to be like' } else { message = 'Expected ' + actual + ' not to be like' } return { pass: pass, message: message }; } // end compare }; } // end toBeLike function }); });
使用方式函數
describe('to be like suite', () => { function noop () {} function noop2 () {} it('assert to be like', function () { expect('f').toBeLike('a'); expect(null).toBeLike(undefined); expect({ msg: 'first msg', cb: noop }).toBeLike({ msg: 'second msg', cb: noop2 }); }); it('assert no to be like', function () { expect(1).not.toBeLike('1'); expect(noop).not.toBeLike(); }); });