命名空間:非同步比對器

非同步比對器

執行於實際值(一個承諾)並傳回一個承諾的非同步比對器。

大多數非同步比對器會無盡期地等待承諾解決或拒絕,若從未發生,將導致規格逾時。如果您期望承諾在呼叫比對器時已解決或拒絕,您可以使用 async-matchers#already 修改器以更快的失敗與更有用的訊息取得。

注意:規格必須等待每個非同步比對器的結果、傳回比對器傳回的承諾,或傳回衍生自比對器傳回承諾的承諾。否則比對器將不會在規格完成前評估。

範例

// Good
await expectAsync(aPromise).toBeResolved();
// Good
return expectAsync(aPromise).toBeResolved();
// Good
return expectAsync(aPromise).toBeResolved()
 .then(function() {
   // more spec code
 });
// Bad
expectAsync(aPromise).toBeResolved();

成員

already :async-matchers

若實際狀態為待處理,則盡快失敗。否則評估比對器。

類型
  • 3.8.0
範例
await expectAsync(myPromise).already.toBeResolved();
return expectAsync(myPromise).already.toBeResolved();

not :async-matchers

反轉依此 expectAsync 執行之比對器

類型
範例
await expectAsync(myPromise).not.toBeResolved();
return expectAsync(myPromise).not.toBeResolved();

方法

(async) toBePending()

期望承諾為待處理,也就是說承諾尚無解決或拒絕。

  • 3.6
範例
await expectAsync(aPromise).toBePending();

(async) toBeRejected()

期望承諾被拒絕。

  • 3.1.0
範例
await expectAsync(aPromise).toBeRejected();
return expectAsync(aPromise).toBeRejected();

(async) toBeRejectedWith(expected)

期望承諾以等同於預期的值拒絕,以深度相等度比對。

參數
名稱 類型 說明
expected 物件

期望承約束以之拒絕的值

  • 3.3.0
範例
await expectAsync(aPromise).toBeRejectedWith({prop: 'value'});
return expectAsync(aPromise).toBeRejectedWith({prop: 'value'});

(async) toBeRejectedWithError(expectedopt, messageopt)

期望承諾以符合預期的值拒絕

參數
名稱 類型 屬性 說明
expected 錯誤 <選用>

thrown 物件必須是其執行個體的 錯誤 建構函數。如果未提供,將會使用 錯誤

訊息 正規表示法 | 字串 <選用>

應設定於拋出 錯誤 的訊息

  • 3.5.0
範例
await expectAsync(aPromise).toBeRejectedWithError(MyCustomError, 'Error message');
await expectAsync(aPromise).toBeRejectedWithError(MyCustomError, /Error message/);
await expectAsync(aPromise).toBeRejectedWithError(MyCustomError);
await expectAsync(aPromise).toBeRejectedWithError('Error message');
return expectAsync(aPromise).toBeRejectedWithError(/Error message/);

(async) toBeResolved()

期望承諾已解決。

  • 3.1.0
範例
await expectAsync(aPromise).toBeResolved();
return expectAsync(aPromise).toBeResolved();

(async) toBeResolvedTo(expected)

期望承諾已解決至等同於預期的值,以深度相等度比對。

參數
名稱 類型 說明
expected 物件

期望承約束以之解決的值

  • 3.1.0
範例
await expectAsync(aPromise).toBeResolvedTo({prop: 'value'});
return expectAsync(aPromise).toBeResolvedTo({prop: 'value'});

withContext(message) → {非同步比對函數}

expectAsync 新增一些內容

參數
名稱 類型 說明
訊息 字串

在非同步比對函數失敗時顯示的額外內容

  • 3.3.0
傳回值
類型
非同步比對器