執行環境套件管理器打包器測試執行器程式碼檢查格式化指南參考部落格意見回饋

使用 Bun 測試執行器將測試標記為“todo”

要提醒自己稍後編寫測試,請使用 test.todo 函式。不要求提供實現。

test.test.ts
import { test } from "bun:test";

// write this later
test.todo("parses durations like 1h30m");

bun test 的輸出會報告 todo 測試的數量。

terminal
bun test
Text
test.test.ts:
✎ parses durations like 1h30m

 0 pass
 1 todo
 0 fail
Ran 1 test across 1 file. [6.00ms]

你也可以在被測試的程式碼尚未編寫之前先寫測試。這裡的 parseDuration 仍是一個存根,而 todo 測試記錄了它最終應該實現的功能。bun test 也會將此測試報告為 todo,但不會執行它的測試體。

test.test.ts
import { test, expect } from "bun:test";

// Not written yet; the todo test below says what it should do.
function parseDuration(input: string): number {
  throw new Error("not implemented");
}

test.todo("parses durations like 1h30m", () => {
  expect(parseDuration("1h30m")).toBe(5400);
});

傳入 --todo 以執行 todo 測試的測試體。todo 測試_預期會失敗_:由於 parseDuration 尚未實現,Bun 會列印錯誤,但仍將該測試計為 todo,並以程式碼 0 退出。

terminal
bun test --todo
Text
test.test.ts:
1 | import { test, expect } from "bun:test";
2 |
3 | // Not written yet; the todo test below says what it should do.
4 | function parseDuration(input: string): number {
5 |   throw new Error("not implemented");
                                       ^
error: not implemented
      at parseDuration (/path/to/test.test.ts:5:36)
      at <anonymous> (/path/to/test.test.ts:9:10)
✎ parses durations like 1h30m [0.16ms]

 0 pass
 1 todo
 0 fail
Ran 1 test across 1 file. [5.00ms]

實現 parseDuration 並使測試體通過後,bun test --todo 會將該測試報告為失敗,並以非零程式碼退出。這意味著你應該移除 .todo,將其變為常規測試。

Text
test.test.ts:
✗ parses durations like 1h30m [0.19ms]
  ^ this test is marked as todo but passes. Remove `.todo` if tested behavior now works

 0 pass
 1 fail
 1 expect() calls
Ran 1 test across 1 file. [5.00ms]
$ echo $?
1 # 這是前一個命令的退出程式碼

另見: