簡體   English   中英

如何強制 mongodb 錯誤以開玩笑的方式測試 try and catch 條件

[英]How can I force a mongodb error to test with jest the try and catch condition

我需要測試一個代碼,但考慮到我的代碼是結構化的條件,我不能發生錯誤並且 go 到 catch 條件。

這不會發生,因為我對 Promise 的拋出條件。 全部都在另一個文件中,該文件處理 promise 的返回

export const userExists = async (name, phone) => {
  try {
    const userExists = await Promise.all([User.findOne({ name}), User.findOne({ phone})]);
    if (userExists.some(el => !!el)) return true;
    else return false;
  } catch (error) {
    return error;
  }
};

在您的 try 塊內添加:

throw new Error("Test Error")

像這樣:

export const userExists = async (name, phone) => {
  try {
    throw new Error("Test Error");
    const userExists = await Promise.all([User.findOne({ name}), User.findOne({ phone})]);
    if (userExists.some(el => !!el)) return true;
    else return false;
  } catch (error) {
    return error;
  }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM