簡體   English   中英

反應功能組件的笑話測試用例失敗

[英]jest test case is failing for react functional component

當我運行這個測試時,我的代碼失敗了。 我努力了

await expect(API().isFileAvailable).rejects.toEqual(Promise.reject(new Error('Bad input')));

但這不起作用

File: async (a: string, b: string | undefined, c: string | undefined) => {
        if (!a&& !b) {
            return Promise.reject(new Error('Bad input'));
        }
}

在此處輸入圖像描述

根據 Jest 的文檔, rejects關鍵字應該與toThrow()一起使用(它是toThrowError()的別名)。

您可以直接使用字符串來匹配錯誤消息,或正則表達式,或Error object。

您的斷言應適用於以下內容:

await expect(API().isFileAvailable).rejects.toThrow('Bad input');

此外,您可能應該在代碼中使用throw 使用Promise.reject()可讀性較差,而異步/等待的好處正是在您拋出錯誤時自動拒絕。

File: async (a: string, b: string | undefined, c: string | undefined) => {
        if (!a&& !b) {
            // return Promise.reject(new Error('Bad input'));
            throw new Error('Bad input');
        }
}

暫無
暫無

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

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