Bun

Bun v0.6.6


Ashcon Partovi · 2023 年 5 月 31 日

我們正在招募 C/C++ 和 Zig 工程師,一同打造 JavaScript 的未來! 加入我們的團隊 →

我們最近發布了 Bun 的許多變更,這裡為您回顧一下,以防您錯過了

  • v0.6.0 - 推出 bun build,Bun 的全新 JavaScript 打包器。
  • v0.6.2 - 效能提升:JSON.parse 快 20%,Proxyarguments 最多快 2 倍。
  • v0.6.3 - 實作 node:vm,並對 node:httpnode:tls 進行了大量修復。
  • v0.6.4 - 實作 require.cacheprocess.env.TZ,以及 bun test 快 80%。
  • v0.6.5 - 原生支援 CommonJS 模組(先前,Bun 會將 CJS 轉譯為 ESM),Vue 支援

現在,我們發布了 Bun v0.6.6,對 bun test 進行了重大改進,包括 Github Actions 支援、支援 test.only()test.if()describe.skip(),以及 15 個以上的 expect() matcher;以及使用 fetch() 的串流檔案上傳。

安裝 Bun

curl
npm
brew
docker
curl
curl -fsSL https://bun.dev.org.tw/install | bash
npm
npm install -g bun
brew
brew tap oven-sh/bun
brew install bun
docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun

升級 Bun

bun upgrade

Github Actions 中的 bun test

Bun 現在將偵測 bun test 是否在 Github Action 中執行。

首先,它會將日誌輸出中的測試依檔案摺疊,使其更易於閱讀。

bun test in github action logs

接下來,它將使用錯誤的名稱、訊息和堆疊追蹤來註解錯誤。

bun test in github annotations

您也不需要安裝任何外掛程式或擴充功能即可使其運作。Bun 將讀取 GITHUB_ACTIONS 環境變數並檢查是否設定為 true

bun test --only

您現在可以指定 --only,僅執行標記為 test.only()describe.only() 的測試。當您想要偵錯檔案中的特定測試時,這非常有用。

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

describe.only("describe #1", () => {
  test("test #1", () => {
    // runs
  });
  test.skip("test #2", () => {
    // does not run
  });
});

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

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

bun test --todo

除了 test.skip() 之外,您還可以使用 test.todo()describe.todo() 定義測試。預設情況下,這些測試不會執行,除非您指定 --todo。這對於尚未實作、損壞或在某些方面不穩定的測試非常有用。

import { test } from "bun:test";

test("test #1", () => {
  // runs
});

test.todo("test #2"); // never runs

test.todo("test #3", () => {
  // only runs if --todo is passed
});

test.if()describe.if()

您現在可以根據布林值指定條件式測試。如果您想要反向行為,也可以使用 test.skipIf()

因此,您可以不用這樣做

import { it } from "bun:test";

const macOS = process.arch === "darwin";
const test = macOS ? test : test.skip;

您可以這樣做

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

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

test.if(macOS)("test #1", () => {
  // runs if macOS
});

test.skipIf(macOS)("test #2", () => {
  // skips if macOS
});

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

test() 中的逾時

在 Jest 中,您可以將第三個引數指定為 test() 的逾時值(以毫秒為單位)。在 Vitest 中,您也可以指定具有 timeout 屬性的物件。在 Bun 中,您現在可以同時做到這兩者。

import { test } from "bun:test";
import { sleep } from "bun";

test("timeout #1", async () => {
  await sleep(10);
}, 1);

test(
  "timeout #2",
  async () => {
    await sleep(10);
  },
  { timeout: 1 },
);

預設逾時為 5000 毫秒,您可以使用 --timeout 變更預設值。

bun test --timeout 1000 # 1 second

expect() 的新 matcher

我們為 expect() 新增了許多新的 matcher,更多 matcher 即將推出。其中大多數都受到 jest-extended matcher 的支援。

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

test("new matchers", () => {
  expect(null).toBeNil();
  expect(true).toBeBoolean();
  expect(3.14).toBePositive();
  expect(314).toBeInteger();
  expect(3.1).toBeWithin(3, 3.14);
  expect(() => {}).toBeFunction();
  expect(new Date()).toBeDate();
  expect("bunny").toInclude("bun");
  expect("buntime").toStartWith("bun");
});

串流 fetch() 檔案上傳

您現在可以使用 Bun 中的 fetch() 進行串流檔案上傳

import { file } from "bun";

await fetch("https://example.com/", {
  method: "POST",
  body: file("avatar.png"),
});

您也可以使用 FormData 上傳來執行此操作。

import { file } from "bun";

const formData = new FormData();
formData.set("attachment", file("data.csv"));

await fetch("https://example.com/", {
  method: "POST",
  body: formData,
});

當透過 HTTP 傳送大型檔案時,Bun 會自動使用 sendfile() 系統呼叫使其更快。

錯誤修正

我們也進行了各種錯誤修正。

0.6.5 版本中 CommonJS 重寫造成的迴歸

  • 匯入 CommonJS 模組時,--watch--hot 已損壞,現在已修正。
  • 修正了 Object.defineProperty(module, "exports", { get }) 的錯誤,這修正了 supports-color 等套件。

更多錯誤修正

  • 當在 UMD 模組中使用時,打包器轉換會不正確地將 module.exports 重寫為 exports,現在已修正。
  • 修正了 path.parse() 無法正確解析某些路徑的錯誤,感謝 @cirospaciari
  • 修正了 expect(null).toHaveProperty() 造成的崩潰。
  • 修正了在 Bun.listen() 或 Bun.connect() 回呼(或 node:netnode:tls)中擲回錯誤時可能發生的崩潰
  • 修正了 Bun.spawn() 會嘗試將 chdir 設定為空字串的錯誤
  • 修正了在多層巢狀結構中,涉及選用鏈結與計算屬性的錯誤轉譯,感謝 @dylan-conway

更多內容

  • 新增了 --no-macros 標記,並在 node_modules 資料夾中停用了巨集
  • 新增了 macro package.json 匯出條件