跳至主要內容
版本:29.7

使用比對器

Jest 使用「比對器」讓您以不同方式測試值。本文件將介紹一些常用的比對器。如需完整清單,請參閱 expect API 文件

常見比對器

測試值的最快方法是使用精確相等。

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

在此程式碼中,expect(2 + 2) 會傳回「預期」物件。您通常不會對這些預期物件做太多事,只會對它們呼叫比對器。在此程式碼中,.toBe(4) 是比對器。當 Jest 執行時,它會追蹤所有失敗的比對器,以便為您印出漂亮的錯誤訊息。

toBe 使用 Object.is 測試精確相等。如果您要檢查物件的值,請使用 toEqual

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

toEqual 會遞迴檢查物件或陣列的每個欄位。

提示

toEqual 忽略具有 undefined 屬性、undefined 陣列項目、陣列稀疏性或物件類型不匹配的物件金鑰。若要考慮這些,請改用 toStrictEqual

您也可以使用 not 來測試與比對器相反的結果

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

真值

在測試中,您有時需要區分 undefinednullfalse,但有時您不希望將它們視為不同。Jest 包含可讓您明確表達需求的輔助程式。

  • toBeNull 僅比對 null
  • toBeUndefined 僅比對 undefined
  • toBeDefinedtoBeUndefined 的相反
  • toBeTruthy 比對 if 陳述式視為 true 的任何內容
  • toBeFalsy 比對 if 陳述式視為 false 的任何內容

例如

test('null', () => {
const n = null;
expect(n).toBeNull();
expect(n).toBeDefined();
expect(n).not.toBeUndefined();
expect(n).not.toBeTruthy();
expect(n).toBeFalsy();
});

test('zero', () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});

您應使用最精確符合您希望程式碼執行的動作的比對器。

數字

大多數比較數字的方法都有比對器對應項。

test('two plus two', () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);

// toBe and toEqual are equivalent for numbers
expect(value).toBe(4);
expect(value).toEqual(4);
});

對於浮點數相等性,請使用 toBeCloseTo 取代 toEqual,因為您不希望測試依賴於微小的捨入誤差。

test('adding floating point numbers', () => {
const value = 0.1 + 0.2;
//expect(value).toBe(0.3); This won't work because of rounding error
expect(value).toBeCloseTo(0.3); // This works.
});

字串

您可以使用 toMatch 來根據正規表示式檢查字串

test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});

test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});

陣列和可迭代物件

您可以使用 toContain 來檢查陣列或可迭代物件是否包含特定項目

const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'milk',
];

test('the shopping list has milk on it', () => {
expect(shoppingList).toContain('milk');
expect(new Set(shoppingList)).toContain('milk');
});

例外

如果您想要測試特定函式在呼叫時是否會擲回錯誤,請使用 toThrow

function compileAndroidCode() {
throw new Error('you are using the wrong JDK!');
}

test('compiling android goes as expected', () => {
expect(() => compileAndroidCode()).toThrow();
expect(() => compileAndroidCode()).toThrow(Error);

// You can also use a string that must be contained in the error message or a regexp
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
expect(() => compileAndroidCode()).toThrow(/JDK/);

// Or you can match an exact error message using a regexp like below
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
});
提示

拋出例外狀況的函式需要在包裝函式中呼叫,否則 toThrow 斷言會失敗。

更多資訊

這只是其中一部分。如需完整比對器清單,請查看 參考文件

在您瞭解可用的比對器後,下一步是查看 Jest 如何讓您 測試非同步程式碼