跳至主要内容

編寫優質測試

What To Test & Not To Test

  • 只測試自己編寫的代碼
  • 不測試自己無法更動的代碼
    例如第三方套件或函式庫、Browser APIs、Node.js Pacakges 等。

Writing Good Tests - An Overview & Summary

遵循以下重點以編寫優質測試

  • AAA Pattern: Arrange, Act, Assert
  • Only test one thing per test
  • Focus on the essence of a test when arranging
    力求精簡、不複雜化。例如測試加總函式時,只需測試兩個值相加,不需測試五個值相加。
  • Keep your number of assertions ("expects") low

Only Test "One Thing"

一項測試只測一件事,一件事指一個功能或一個行為。

A Word About Code Coverage

Vitest 內建測試覆蓋率檢視工具,使用說明如下:
Vitest Coverage