Jasmine 本身附帶的 Matcher
成員
not :matchers
反轉 expectation
中這個 matchers
類型
- 從
- 1.3.0
範例
expect(something).not.toBe(true);
方法
nothing()
明確 expect
nothing。
- 從
- 2.8.0
範例
expect().nothing();
toBe(expected)
expect
實際值 ===
預期值。
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
物件 | 用於比較的預計值。 |
- 從
- 1.3.0
範例
expect(thing).toBe(realThing);
toBeCloseTo(expected, precisionopt)
expect
the actual value to be within a specified precision of the expected value.
參數
名稱 | 類型 | 屬性 | 預設 | 說明 |
---|---|---|---|---|
expected |
物件 | 用於比較的預計值。 |
||
precision |
數字 | <可選> |
2 | 要核對的小數位數。 |
- 從
- 1.3.0
範例
expect(number).toBeCloseTo(42.2, 3);
toBeDefined()
expect
the actual value to be defined. (Not undefined
)
- 從
- 1.3.0
範例
expect(result).toBeDefined();
toBeFalse()
expect
the actual value to be false
.
- 從
- 3.5.0
範例
expect(result).toBeFalse();
toBeFalsy()
expect
the actual value to be falsy
- 從
- 2.0.0
範例
expect(result).toBeFalsy();
toBeGreaterThan(expected)
expect
the actual value to be greater than the expected value.
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
數字 | 要比較的數值。 |
- 從
- 2.0.0
範例
expect(result).toBeGreaterThan(3);
toBeGreaterThanOrEqual(expected)
expect
the actual value to be greater than or equal to the expected value.
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
數字 | 用於比較的預計值。 |
- 從
- 2.0.0
範例
expect(result).toBeGreaterThanOrEqual(25);
toBeInstanceOf(expected)
expect
the actual to be an instance of the expected class
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
物件 | 要檢查的類別或建構式函式 |
- 從
- 3.5.0
範例
expect('foo').toBeInstanceOf(String);
expect(3).toBeInstanceOf(Number);
expect(new Error()).toBeInstanceOf(Error);
toBeLessThan(預期值)
expect
預計實際值會小於預期值。
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
數字 | 用於比較的預計值。 |
- 從
- 2.0.0
範例
expect(result).toBeLessThan(0);
toBeLessThanOrEqual(預期值)
expect
預計實際值會小於或等於預期值。
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
數字 | 用於比較的預計值。 |
- 從
- 2.0.0
範例
expect(result).toBeLessThanOrEqual(123);
toBeNaN()
expect
預計實際值會是 NaN
(非數值)。
- 從
- 1.3.0
範例
expect(thing).toBeNaN();
toBeNegativeInfinity()
expect
預計實際值會是 -Infinity
(負無窮大)。
- 從
- 2.6.0
範例
expect(thing).toBeNegativeInfinity();
toBeNull()
expect
預計實際值會是 null
。
- 從
- 1.3.0
範例
expect(result).toBeNull();
toBePositiveInfinity()
expect
預計實際值會是 Infinity
(正無窮大)。
- 從
- 2.6.0
範例
expect(thing).toBePositiveInfinity();
toBeTrue()
expect
預計實際值會是 true
。
- 從
- 3.5.0
範例
expect(result).toBeTrue();
toBeTruthy()
expect
預計實際值會是真值。
- 從
- 2.0.0
範例
expect(thing).toBeTruthy();
toBeUndefined()
expect
預計實際值會是 undefined
。
- 從
- 1.3.0
範例
expect(result).toBeUndefined():
toContain(預期值)
expect
預計實際值會包含特定值。
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
物件 | 要尋找的值。 |
- 從
- 2.0.0
範例
expect(array).toContain(anElement);
expect(string).toContain(substring);
toEqual(預期值)
expect
預計實際值會和預期值相等,使用深度相等性比較。
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
物件 | 預期值 |
- 從
- 1.3.0
範例
expect(bigObject).toEqual({"foo": ['bar', 'baz']});
toHaveBeenCalled()
- 從
- 1.3.0
範例
expect(mySpy).toHaveBeenCalled();
expect(mySpy).not.toHaveBeenCalled();
toHaveBeenCalledBefore(預期值)
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
Spy |
- 從
- 2.6.0
範例
expect(mySpy).toHaveBeenCalledBefore(otherSpy);
toHaveBeenCalledOnceWith()
參數
類型 | 屬性 | 說明 |
---|---|---|
物件 | <可重複> |
要尋找的參數 |
- 從
- 3.6.0
範例
expect(mySpy).toHaveBeenCalledOnceWith('foo', 'bar', 2);
toHaveBeenCalledTimes(預期值)
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
數字 | 要尋找的呼叫次數。 |
- 從
- 2.4.0
範例
expect(mySpy).toHaveBeenCalledTimes(3);
toHaveBeenCalledWith()
參數
類型 | 屬性 | 說明 |
---|---|---|
物件 | <可重複> |
要尋找的參數 |
- 從
- 1.3.0
範例
expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2);
toHaveClass(預期)
expect
預期的實際值為具有預期的類別的 DOM 元素
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
物件 | 要測試的類別名稱 |
- 從
- 3.0.0
範例
const el = document.createElement('div');
el.className = 'foo bar baz';
expect(el).toHaveClass('bar');
toHaveSize(預期)
expect
預期的實際大小等於預期,使用類似陣列的長度或物件的鍵值大小。
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
物件 | 預期的長度 |
- 從
- 3.6.0
範例
array = [1,2];
expect(array).toHaveSize(2);
toHaveSpyInteractions()
expect
預期(SpyObj
)間諜被呼叫。
- 從
- 4.1.0
範例
expect(mySpyObj).toHaveSpyInteractions();
expect(mySpyObj).not.toHaveSpyInteractions();
toMatch(預期)
expect
預期的實際值與正規表示式相符
參數
名稱 | 類型 | 說明 |
---|---|---|
expected |
RegExp | 字串 | 在字串中要尋找的值。 |
- 從
- 1.3.0
範例
expect("my string").toMatch(/string$/);
expect("other string").toMatch("her");
toThrow(預期opt)
expect
一個函式會拋出
某些東西。
參數
名稱 | 類型 | 屬性 | 說明 |
---|---|---|---|
expected |
物件 | <可選> |
應該拋出的值。如果未提供,只會檢查是否拋出任何東西。 |
- 從
- 2.0.0
範例
expect(function() { return 'things'; }).toThrow('foo');
expect(function() { return 'stuff'; }).toThrow();
toThrowError(預期opt, 訊息opt)
expect
一個函式會拋出
一個Error
。
參數
名稱 | 類型 | 屬性 | 說明 |
---|---|---|---|
expected |
錯誤 | <可選> |
|
訊息 |
RegExp | 字串 | <可選> |
應該設定為拋出的 |
- 從
- 2.0.0
範例
expect(function() { return 'things'; }).toThrowError(MyCustomError, 'message');
expect(function() { return 'things'; }).toThrowError(MyCustomError, /bar/);
expect(function() { return 'stuff'; }).toThrowError(MyCustomError);
expect(function() { return 'other'; }).toThrowError(/foo/);
expect(function() { return 'other'; }).toThrowError();
toThrowMatching(謂詞)
expect
一個函式會拋出
符合謂詞的某些東西。
參數
名稱 | 類型 | 說明 |
---|---|---|
謂詞 |
函式 | 將拋出的例外狀況作為其參數的函式,如果符合則回傳 true。 |
- 從
- 3.0.0
範例
expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });
withContext(訊息) → {matches}
為 預期
的 match 失敗加入一些要包含的 context,以便與相似的預期更容易區分。
參數
名稱 | 類型 | 說明 |
---|---|---|
訊息 |
字串 | 當 match 失敗時要顯示的額外 context |
- 從
- 3.3.0
傳回
- 類型
- matchers
範例
expect(things[0]).withContext('thing 0').toEqual('a');
expect(things[1]).withContext('thing 1').toEqual('b');