Jest中常用斷言歸納

Jest中常用的匹配器

toBe()匹配器:

toBe匹配器用來比較原始值或檢查對象實例的引用一致性,類似於 ‘Object.is’ 方法和 ‘===’.

// toBe匹配器
test('測試 結果是否等於 "abc"', () => {
  const a = 'abc';
  expect(a).toBe('abc');
})
toEqual()匹配器:

用來匹配內容是否相等,會遞歸的檢查對象的所有屬性和屬性值是否相等。(如對象內容是否相等)

// 使用toEqual匹配器測試對象內容是否相等
test('測試對象內容是否相等', () => {
  const a = { b: 1 };
  expect(a).toEqual({ b: 1 });
})
toHaveLength(number)匹配器

匹配對象的長度是否爲指定值。

//toHaveLength(number)
const arr = [1, 2, 3];
test('測試 arr數組的長度 是否爲3', () => {   
  expect(arr).toHaveLength(3);
})

=>和真假相關的匹配器

toBeNull匹配器

匹配某些內容是否爲null值。

// toBeNull匹配器
const val = null;
test('測試 null 是否爲 空', () => {   
  expect(val).toBeNull();
})
toBeUndefined匹配器

檢測一個結果是undefined。

// toBeUndefined匹配器
const val = undefined;
test('測試 結果 是否爲 undefined', () => {   
  expect(val).toBeUndefined();
}) 
toBeDefined匹配器

檢查一個結果不是undefined。

// toBeDefined匹配器
const val = 1; // 或者 val = null 也可通過測試
test('測試 結果 是否爲 undefined', () => {   
  expect(val).toBeDefined();
}) 
toBeTruthy匹配器

檢測結果是否爲true。

// toBeTruthy匹配器
const val = 1;
test('測試 結果 是否爲 true', () => {   
  expect(val).toBeTruthy();
}) 
toBeFalsy匹配器

檢測結果是否爲false。

// toBeFalsy匹配器
const val = 0;
test('測試 結果 是否爲 false', () => {   
  expect(val).toBeFalsy();
}) 
not匹配器
// not匹配器
const val = 1;
test('測試 結果 是否不爲 false', () => {   
  expect(val).not.toBeFalsy();
})

=>和數字相關的匹配器

toBeGreaterThan(number) 匹配器

檢測某個浮點數的結果是否大於指定浮點數。

// .toBeGreaterThan(number) 匹配器
const val = 4;
test('測試 結果 是否大於 指定浮點數', () => {   
  expect(val).toBeGreaterThan(3);
})
toBeGreaterThanOrEqual(number)匹配器

檢測某個浮點數的結果是否大於等於指定浮點數。

// .toBeGreaterThanOrEqual(number)匹配器
const val = 3;
test('測試 結果 是否大於等於 指定浮點數', () => {   
  expect(val).toBeGreaterThanOrEqual(3);
})
toBeLessThan(number) 匹配器

檢測某個浮點數的結果是否小於指定浮點數。

// .toBeLessThan(number) 匹配器
const val = 2;
test('測試 結果 是否小於 指定浮點數', () => {   
  expect(val).toBeLessThan(3);
})
toBeLessThanOrEqual(number) 匹配器

檢測某個浮點數的結果是否小於等於指定浮點數。

// .toBeLessThanOrEqual(number)
const val = 3;
test('測試 結果 是否大於等於 指定浮點數', () => {   
  expect(val).toBeLessThanOrEqual(3);
})
toBeCloseTo(number) 匹配器

檢測浮點數運算結果的精確值是否等於指定的浮點數。

// toBeCloseTo(number)
const a = 0.2;
const b = 0.1;
test('測試 0.1+0.2 是否等於 0.3', () => {   
  expect(a + b).toBeCloseTo(0.3);
})

在js中運算0.1+0.2實際結果爲0.30000000000000004,若想取得正常運算結果比較,可使用toBeCloseTo匹配器。

=>和字符串相關的匹配器

toMatch(str) 匹配器
// toMatch(str)
const val = 'abcdefg';
test('測試 0.1+0.2 是否等於 0.3', () => {   
  expect(val).toMatch('ab');
})

=>和數組相關的匹配器

toContain(str) 匹配器

匹配數組中是否包含某元素。

// toContain(str)
const arr = ['a', 'b', 'c'];
const data = new Set(arr); // Set
test('測試 數組arr中 是否包含 "a"元素', () => {   
  expect(arr).toContain('a');
  // expect(data).toContain('a'); //對Set同樣生效
})
toThrow() 匹配器

檢測函數是否拋出異常。也可以傳入字符串參數,匹配拋出異常的內容。

// 異常
const throwNewErrorFunc = () => {
  throw new Error('this is new error')
}
test('測試 函數是否拋出異常', () => {   
  expect(throwNewErrorFunc).toThrow();
  //expect(throwNewErrorFunc).toThrow('this is new error'); // 匹配拋出異常是否爲'this is new error'
})

Jest響應式測試配置

測試用例

test('測試 7 和 7 是否相匹配', () => {
  // 期待什麼,結果去匹配什麼
  // 這裏的toBe就是一個匹配器
  expect(7).toBe(7);
})

package.json中的scripts配置:

"scripts": {
    "test": "jest"
}

執行測試:npm test

運行npm test單元測試正常,當我們再次或多次修改測試文件時,每次修改都要重新運行npm test,比較麻煩,而且會拖慢我們的開發效率。我們可以通過修改scripts配置,來達到響應式測試的效果:(對package.json中的scripts做如下修改)

"scripts": {
    "test": "jest --watchAll"
}

再次執行:npm test

把jest配置成監聽模式之後,我們再次運行npm test,測試用例就會一直處於監聽的狀態,當再次修改測試代碼時,jest就會自動幫助我們去檢測代碼是否存在異常。(如下)
修改測試代碼:

// 測試用例
test('測試 7 和 7 想匹配', () => {
  // expect(7).toBe(7); // 把匹配器裏的值改爲1
  expect(7).toBe(1); 
})

當我們對測試代碼做出修改之後,jest會立即執行代碼檢測,檢測結果如下:

開啓jest監聽模式之後,每次修改測試代碼,jest就會自動的幫我們去跑每個測試用例,幫我們節省很多時間,可以大大提高我們的開發、測試效率。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章