【Bun中文文档-Test runner】Writing tests

263 阅读2分钟

您可以使用内置的bun:test模块导入与 Jest 类似的 API 来定义测试。长期来看,Bun 旨在实现完全的 Jest 兼容性;目前,支持的expect匹配器集合有限。以下是一些基本的用法示例:

要定义一个简单的测试:

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

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

测试可以使用describe分组:

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); // 测试必须在<500ms内运行

test.skip

使用test.skip跳过单个测试。这些测试将不会被运行。

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

test.skip("wat", () => {
  // TODO: 修复这个
  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()的测试/测试套件。

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

test("test #1", () => {
  // 不运行
});

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

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

以下命令将只执行测试#2 和#3。

$ bun test --only

test.if

要有条件地运行测试,请使用test.if()。如果条件为真,测试将会运行。这对于只应在特定架构或操作系统上运行的测试特别有用。

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

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

要根据某些条件跳过测试,可以使用test.skipIf()describe.skipIf()

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

test.skipIf(macOS)("runs on non-macOS", () => {
  // 如果*不是*macOS,则运行
});

匹配器

Bun 实现了以下匹配器。完全的 Jest 兼容性在路线图上;跟踪进度在这里

🟢.not
🟢.toBe()
🟢.toEqual()
🟢.toBeNull()
🟢.toBeUndefined()
🟢.toBeNaN()
🟢.toBeDefined()
🟢.toBeFalsy()
🟢.toBeTruthy()
🟢.toContain()
🟢.toStrictEqual()
🟢.toThrow()
🟢.toHaveLength()
🟢.toHaveProperty()
🔴.extend
🟢.anything()
🟢.any()
🔴.arrayContaining()
🔴.assertions()
🔴.closeTo()
🔴.hasAssertions()
🔴.objectContaining()
🟢.stringContaining()
🟢.stringMatching()
🔴.addSnapshotSerializer()
🟢.resolves()
🟢.rejects()
🟢.toHaveBeenCalled()
🟢.toHaveBeenCalledTimes()
🔴.toHaveBeenCalledWith()
🔴.toHaveBeenLastCalledWith()
🔴.toHaveBeenNthCalledWith()
🔴.toHaveReturned()
🔴.toHaveReturnedTimes()
🔴.toHaveReturnedWith()
🔴.toHaveLastReturnedWith()
🔴.toHaveNthReturnedWith()
🟢.toBeCloseTo()
🟢.toBeGreaterThan()
🟢.toBeGreaterThanOrEqual()
🟢.toBeLessThan()
🟢.toBeLessThanOrEqual()
🟢.toBeInstanceOf()
🔴.toContainEqual()
🟢.toMatch()
🟢.toMatchObject()
🟢.toMatchSnapshot()
🔴.toMatchInlineSnapshot()
🔴.toThrowErrorMatchingSnapshot()
🔴.toThrowErrorMatchingInlineSnapshot()