Jest測試初學(二) Jest 中的匹配器

2021-09-27 08:22:34 字數 1290 閱讀 2405

讓jest監聽所有測試(test.js)檔案的變化,如果有變化就自動跑測試用例

"scripts": ,
執行測試用例 jest自帶test和expect方法

比較結果的匹配器

test('測試加法 3+7', () => ;

expect(a).tobe();

});

test('測試加法 3+7', () => ;

expect(a).toequal();

});

test('測試加法 3+7', () => );
真假有關的匹配器

test('tobeundefined', () => );
test('tobedefined', () => );
test('tobetruthy', () => );
test('tobefalsy', () => );
test('not', () => );
數字相關的匹配器

test('tobegreaterthan', () => );
test('tobelessthan', () => );
test('tobegreaterthanorequal', () => );
test('tobelessthanorequal', () => );
test('tobecloseto', () => );
string匹配器

test('tomatch', () => );
array set匹配器

test('tocontain', () => );
異常

const thrownewerrorfunc = () => 

test('tothrow', () => );

//兩處的error資訊要一致,否則報錯

Jest測試初學(一) Jest 的簡單配置

執行安裝命令 npx jest init 然後在工程的根目錄下會生成乙個jest.config.js檔案 這個檔案值得細細研究!如果想修改測試覆蓋率報告的資料夾名稱,可以在jest.config.js中配置,修改這一項coveragedirectory coverage 執行命令npx jest c...

Jest測試初學(五) Jest中的鉤子函式

jest中的鉤子函式相當於生命週期函式,在執行測試用例時,有先後呼叫的順序。一 jest常用的鉤子函式 beforeall 在測試用例執行之前執行 beforeeach 在每乙個測試用例執行之前執行 afterall 在測試用例執行結束之後執行 aftereach 在每乙個測試用例執行之後執行 im...

Jest測試框架 Jest匹配器

jest常見的匹配器 修改package.json,加乙個 watchall scripts 測試值的最簡單方法是完全相等。test two plus two is four tobe用於object.is測試完全相等。如果要檢查物件的值,請toequal改用 test object assignm...