[第三週] 用 Jest 寫單元測試


目的:測試 repeat function,要加上module.exports 才可以讓測試檔案引用到。

  1. 安裝 jest
    yarn add --dev jest

    npm install --save-dev jest
  2. 編輯一個名為 index.test.js的檔案
    • require 引用要測試的檔案
    • expect 裡放要 function 要做的事
    • tobe 裡放執行 function 應該要有的結果
  3. 更新 package.json 裡的 <scripts>
    "test": "jest"
  4. 執行 npm run test
    會去找副檔名是 .test.js的檔案來跑測試

    或是執行 npx jest 檔名 (不用更新scripts就可跑測試)
  5. 如果要針對單一個指定的檔案跑測試,更新 package.json 裡的 <scripts>
    "test": "jest 欲測試的檔名"
  6. 可以多增加測試條件

  7. 加上 describe() 可以更結構化

#Jest #Unit Test






你可能感興趣的文章

Week 21 學習的第二週

Week 21 學習的第二週

深入學習 LSD-SLAM-1

深入學習 LSD-SLAM-1

字串回傳整數 parseInt(string, radix)

字串回傳整數 parseInt(string, radix)






留言討論