亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Jest - 斷言異步函數拋出測試失敗

Jest - 斷言異步函數拋出測試失敗

catspeake 2022-01-01 20:08:15
得到以下失敗的測試用例,我不知道為什么:foo.jsasync function throws() {  throw 'error';}async function foo() {  try {    await throws();  } catch(e) {    console.error(e);    throw e;  }}測試.jsconst foo = require('./foo');describe('foo', () => {  it('should log and rethrow', async () => {    await expect(foo()).rejects.toThrow();  });});我希望 foo 拋出但由于某種原因它只是解決并且測試失?。篎AILED foo ? should log and rethrow - Received function did not throw可能缺少異步等待拋出行為的一些基本細節。
查看完整描述

3 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

我認為你需要的是檢查被拒絕的錯誤


const foo = require('./foo');

describe('foo', () => {

  it('should log and rethrow', async () => {

    await expect(foo()).rejects.toEqual('error');

  });

});


查看完整回答
反對 回復 2022-01-01
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

似乎這是一個已知的錯誤:https : //github.com/facebook/jest/issues/1700


這雖然有效:


describe('foo', () => {

  it('should log and rethrow', async () => {

    await expect(foo()).rejects.toEqual('error')

  });

});


查看完整回答
反對 回復 2022-01-01
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

當我不想使用toEqualor toBe(如其他正確答案)時,我會使用此代碼。相反,我使用toBeTruthy.


async foo() {

  throw "String error";

}


describe('foo', () => {

  it('should throw a statement', async () => {

    await expect(foo()).rejects.toBeTruthy();

  });

});


查看完整回答
反對 回復 2022-01-01
  • 3 回答
  • 0 關注
  • 406 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號