Bun

撰寫測試

使用從內建 bun:test 模組匯入的類似 Jest 的 API 定義測試。長遠來看,Bun 旨在完全相容於 Jest;目前,支援 有限的 expect 比對器。

基本用法

定義一個簡單的測試

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

test("2 + 2", () => {
  expect(2 + 2).toBe(4);
});

Jest 風格的全局變數

測試可以使用 describe 分組到套件中。

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

describe("arithmetic", () => {
  test("2 + 2", () => {
    expect(2 + 2).toBe(4);
  });

  test("2 * 2", () => {
    expect(2 * 2).toBe(4);
  });
});

測試可以是 async

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

test("2 * 2", async () => {
  const result = await Promise.resolve(2 * 2);
  expect(result).toEqual(4);
});

或者,使用 done 回呼函式來表示完成。如果你在測試定義中將 done 回呼函式作為參數包含,你必須呼叫它,否則測試將會掛起。

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

test("2 * 2", done => {
  Promise.resolve(2 * 2).then(result => {
    expect(result).toEqual(4);
    done();
  });
});

逾時

可以選擇將每個測試的逾時時間(以毫秒為單位)指定為 test 的第三個參數。

import { test } from "bun:test";

test("wat", async () => {
  const data = await slowOperation();
  expect(data).toBe(42);
}, 500); // test must run in <500ms

test.skip

使用 test.skip 跳過個別測試。這些測試不會執行。

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

test.skip("wat", () => {
  // TODO: fix this
  expect(0.1 + 0.2).toEqual(0.3);
});

test.todo

使用 test.todo 將測試標記為待辦事項。這些測試執行,並且測試執行器會預期它們會失敗。如果它們通過,系統會提示你將其標記為常規測試。

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

test.todo("fix this", () => {
  myTestFunction();
});

若要專門執行標記為待辦事項的測試,請使用 bun test --todo

bun test --todo

test.only

若要執行特定測試或測試套件,請使用 test.only()describe.only()。宣告後,執行 bun test --only 將只執行已標記為 .only() 的測試/套件。在宣告 test.only() 的情況下執行 bun test 而沒有 --only 選項,將導致執行給定套件中的所有測試,直到 .only() 的測試。describe.only() 在兩種執行情境中運作方式相同。

import { test, describe } from "bun:test";

test("test #1", () => {
  // does not run
});

test.only("test #2", () => {
  // runs
});

describe.only("only", () => {
  test("test #3", () => {
    // runs
  });
});

以下命令將只執行測試 #2 和 #3。

bun test --only

以下命令將只執行測試 #1、#2 和 #3。

bun test

test.if

若要根據條件執行測試,請使用 test.if()。如果條件為真,測試將執行。這對於僅應在特定架構或作業系統上執行的測試特別有用。

test.if(Math.random() > 0.5)("runs half the time", () => {
  // ...
});

const macOS = process.arch === "darwin";
test.if(macOS)("runs on macOS", () => {
  // runs if macOS
});

test.skipIf

若要根據某些條件跳過測試,請使用 test.skipIf()describe.skipIf()

const macOS = process.arch === "darwin";

test.skipIf(macOS)("runs on non-macOS", () => {
  // runs if *not* macOS
});

test.todoIf

如果你想將測試標記為待辦事項,請使用 test.todoIf()describe.todoIf()。仔細選擇 skipIftodoIf 可以顯示差異,例如「此目標無效」和「已規劃但尚未實作」的意圖。

const macOS = process.arch === "darwin";

// TODO: we've only implemented this for Linux so far.
test.todoIf(macOS)("runs on posix", () => {
  // runs if *not* macOS
});

test.each

若要傳回測試表格中多個案例的函式,請使用 test.each

const cases = [
  [1, 2, 3],
  [3, 4, 5],
];

test.each(cases)("%p + %p should be %p", (a, b, expected) => {
  // runs once for each test case provided
});

有許多選項可供格式化案例標籤,具體取決於其類型。

%ppretty-format
%s字串
%d數字
%i整數
%f浮點數
%jJSON
%o物件
%#測試案例索引
%%單一百分比符號 (%)

比對器

Bun 實作下列比對器。完整的 Jest 相容性已列入路線圖;在此追蹤進度 here