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

- 安裝 jest
yarn add --dev jest
或
npm install --save-dev jest - 編輯一個名為 index.test.js的檔案
- 用
require引用要測試的檔案 - expect 裡放要 function 要做的事
- tobe 裡放執行 function 應該要有的結果

- 用
- 更新
package.json裡的 <scripts>
"test": "jest"

- 執行
npm run test
會去找副檔名是 .test.js的檔案來跑測試

或是執行npx jest 檔名(不用更新scripts就可跑測試)

- 如果要針對單一個指定的檔案跑測試,更新
package.json裡的 <scripts>
"test": "jest 欲測試的檔名"

可以多增加測試條件


加上 describe() 可以更結構化




