Jest 使用匹配器

2021-09-23 20:09 更新

使用匹配器

Jest使用匹配器(Matchers)讓你可以運(yùn)用各種方式測(cè)試你的代碼。 這篇文檔將向你介紹一些常用的匹配器, 在expect API可以看到完整的列表。

普通匹配器

最簡(jiǎn)單的測(cè)試值的方法是看是否精確匹配。

  1. test('two plus two is four',()=>{
  2. expect(2+2).toBe(4);
  3. });

在此代碼中,?expect (2 + 2)? 返回一個(gè)?expect?的對(duì)象。 你通常不會(huì)對(duì)這些?expect?對(duì)象調(diào)用過多的匹配器。 在此代碼中,?.toBe(4)? 是匹配器。 當(dāng) Jest 運(yùn)行時(shí),它會(huì)跟蹤所有失敗的匹配器,以便它可以為你打印出很好的錯(cuò)誤消息。

?toBe? 使用 ?Object.is ?來(lái)測(cè)試精確相等。 如果想要檢查對(duì)象的值,請(qǐng)使用 ?toEqual?代替:

  1. test('object assignment',()=>{
  2. const data ={one:1};
  3. data['two']=2;
  4. expect(data).toEqual({one:1, two:2});
  5. });

?toEqual遞歸檢查對(duì)象或數(shù)組的每個(gè)字段。

你還可以測(cè)試相反的匹配︰

  1. test('adding positive numbers is not zero',()=>{
  2. for(let a =1; a <10; a++){
  3. for(let b =1; b <10; b++){
  4. expect(a + b).not.toBe(0);
  5. }
  6. }
  7. });

真實(shí)性

在測(cè)試中,有時(shí)候你需要區(qū)分 ?undefined?, ?null?, 和?false?, 但有時(shí)你不想用不同等方式來(lái)對(duì)待它們。Jest 讓你明確你想要什么。

  • ?toBeNull? 只匹配 ?null?
  • ?toBeUndefined ?只匹配 ?undefined?
  • ?toBeDefined ?與 ?toBeUndefined ?相反
  • ?toBeTruthy ?匹配任何 ?if? 語(yǔ)句為真
  • ?toBeFalsy ?匹配任何 ?if ?語(yǔ)句為假

例如:

  1. test('null',()=>{
  2. const n =null;
  3. expect(n).toBeNull();
  4. expect(n).toBeDefined();
  5. expect(n).not.toBeUndefined();
  6. expect(n).not.toBeTruthy();
  7. expect(n).toBeFalsy();
  8. });
  9. test('zero',()=>{
  10. const z =0;
  11. expect(z).not.toBeNull();
  12. expect(z).toBeDefined();
  13. expect(z).not.toBeUndefined();
  14. expect(z).not.toBeTruthy();
  15. expect(z).toBeFalsy();
  16. });

你應(yīng)該用匹配器將你的代碼和你想要的內(nèi)容進(jìn)行最精確的匹配。

數(shù)字

大多數(shù)的比較數(shù)字有等價(jià)的匹配器。

  1. test('two plus two',()=>{
  2. const value =2+2;
  3. expect(value).toBeGreaterThan(3);
  4. expect(value).toBeGreaterThanOrEqual(3.5);
  5. expect(value).toBeLessThan(5);
  6. expect(value).toBeLessThanOrEqual(4.5);
  7. // toBe and toEqual are equivalent for numbers
  8. expect(value).toBe(4);
  9. expect(value).toEqual(4);
  10. });

對(duì)于比較浮點(diǎn)數(shù)相等,應(yīng)當(dāng)使用 ?toBeCloseTo ?而不是 ?toEqual?,因?yàn)槟悴幌M麥y(cè)試取決于一個(gè)小小的舍入誤差。

  1. test('兩個(gè)浮點(diǎn)數(shù)字相加',()=>{
  2. const value =0.1+0.2;
  3. //expect(value).toBe(0.3); 這句會(huì)報(bào)錯(cuò),因?yàn)楦↑c(diǎn)數(shù)有舍入誤差
  4. expect(value).toBeCloseTo(0.3);// 這句可以運(yùn)行
  5. });

字符串

你可以檢查對(duì)具有 ?toMatch ?正則表達(dá)式的字符串︰

  1. test('there is no I in team',()=>{
  2. expect('team').not.toMatch(/I/);
  3. });
  4. test('but there is a "stop" in Christoph',()=>{
  5. expect('Christoph').toMatch(/stop/);
  6. });

數(shù)組和可迭代對(duì)象

你也可以通過 ?toContain?來(lái)檢查一個(gè)數(shù)組或可迭代對(duì)象是否包含某個(gè)特定項(xiàng):

  1. const shoppingList =[
  2. 'diapers',
  3. 'kleenex',
  4. 'trash bags',
  5. 'paper towels',
  6. 'beer',
  7. ];
  8. test('the shopping list has beer on it',()=>{
  9. expect(shoppingList).toContain('beer');
  10. expect(newSet(shoppingList)).toContain('beer');
  11. });

例外

如果要測(cè)試特定函數(shù)在調(diào)用時(shí)是否拋出錯(cuò)誤,請(qǐng)使用?toThrow?.

  1. function compileAndroidCode(){
  2. thrownewError('you are using the wrong JDK');
  3. }
  4. test('compiling android goes as expected',()=>{
  5. expect(compileAndroidCode).toThrow();
  6. expect(compileAndroidCode).toThrow(Error);
  7. // You can also use the exact error message or a regexp
  8. expect(compileAndroidCode).toThrow('you are using the wrong JDK');
  9. expect(compileAndroidCode).toThrow(/JDK/);
  10. });

更多內(nèi)容

以上這些內(nèi)容只是淺嘗輒止,想要了解更多有關(guān)匹配器的完整列表,請(qǐng)查閱參考文檔。

一旦你學(xué)會(huì)了如何使用匹配器后,接下來(lái)可以學(xué)習(xí) Jest 是如何測(cè)試異步代碼的。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)