簡體   English   中英

javascript中的軟斷言

[英]Soft assertions in javascript

我有兩個后端項目 P1 和 P2。 來自 P1 的數據必須經過中間件處理后流入 P2。 我正在編寫這個中間件,我必須創建一個 E2E 測試模塊。

我將有 100 個測試用例,每個用例可能有 3 或 4 個期望語句。 chai 'expect' 函數是硬斷言的一種形式。 如何在 javascript 中獲得軟斷言。 基本上,測試用例將運行所有 3 或 4 個期望語句並報告哪個失敗了。

Chai 不允許軟斷言,這違背了他們的斷言哲學。 嘗試使用庫https://www.npmjs.com/package/soft-assert

我們需要類似的東西,而 Raymond 提出的庫對我們來說還不夠(我們不想改變斷言庫,而且庫缺乏我們需要的很多斷言類型),所以我寫了這個我認為完美的答案問題: https : //github.com/alfonso-presa/soft-assert

使用這個軟斷言庫,您可以包裝其他資產庫(如您要求的 chai 期望),以便您可以在測試中執行軟斷言和硬斷言。 這里有一個例子:

const { proxy, flush } = require("@alfonso-presa/soft-assert");
const { expect } = require("chai");
const softExpect = proxy(expect);

describe("something", () => {
    it("should capture exceptions with wrapped chai expectation library", () => {
        softExpect("a").to.equal("b");
        softExpect(false).to.be.true;
        softExpect(() => {}).to.throw("Error");
        softExpect(() => {throw new Error();}).to.not.throw();
        try {
            //This is just to showcase, you should not try catch the result of flush.
            flush();
            //As there are assertion errors above this will not be reached
            expect(false).toBeTruthy();
        } catch(e) {
            expect(e.message).toContain("expected 'a' to equal 'b'");
            expect(e.message).toContain("expected false to be true");
            expect(e.message).toContain("to throw an error");
            expect(e.message).toContain("to not throw an error but 'Error' was thrown");
        }
    });
});

暫無
暫無

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

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