method

test.Test.runIf

condition: boolean
): Test<T>;

Runs this test, if condition is true.

Referenced types

interface Test<T extends ReadonlyArray<unknown>>

Runs a test.

test("can check if using Bun", () => {
  expect(Bun).toBeDefined();
});

test("can make a fetch() request", async () => {
  const response = await fetch("https://example.com/");
  expect(response.ok).toBe(true);
});

test("can set a timeout", async () => {
  await Bun.sleep(100);
}, 50); // or { timeout: 50 }
  • afterAll: (fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: HookOptions) => void
  • afterEach: (fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: HookOptions) => void
  • aroundAll: (fn: (runSuite: () => Promise<void>, context: object, suite: TestSuite) => unknown, options?: HookOptions) => void
  • aroundEach: (fn: (runTest: () => Promise<void>, context: TestContext, suite: TestSuite) => unknown, options?: HookOptions) => void
  • beforeAll: (fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: HookOptions) => void
  • beforeEach: (fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: HookOptions) => void
  • concurrent: Test<T>

    Runs the test concurrently with other concurrent tests.

  • extend: ExtendTest<object>

    Returns a test whose callbacks receive the TestContext with fixtures added to it.

    A fixture is set up for the tests that destructure it, and for the returned test's beforeEach() / afterEach() hooks that destructure it. It's torn down after the test.

    const test = base.extend<{ server: Server }>({
      server: async ({}, use) => {
        const server = Bun.serve({ port: 0, fetch: () => new Response("ok") });
        await use(server);
        await server.stop();
      },
    });
    
    test("responds", async ({ server }) => {
      expect(await (await fetch(server.url)).text()).toBe("ok");
    });
  • failing: Test<T>

    Marks this test as failing.

    Use test.failing for a test you expect to fail. The result is inverted: the test passes if it throws, and fails if it does not.

    test.failing is similar to test.todo except that it always runs, regardless of the --todo flag.

  • fails: Test<T>

    Marks this test as failing, like Vitest's test.fails.

    The test passes if any part of it fails: its callback, a beforeEach or afterEach hook, the timeout, or expect.assertions(). With test.failing, only an error from the callback counts.

  • only: Test<T>

    Skips all other tests, except this test.

  • sequential: Test<T>

    Forces the test to run serially (not in parallel).

  • serial: Test<T>

    Forces the test to run serially (not in parallel), even when the --concurrent flag is used.

  • skip: (label: string) => void & Test<T>

    Skips this test.

  • todo: (label: string) => void & Test<T>

    Marks this test as to be written or to be fixed.

    These tests only run when the --todo flag is passed. With the flag, a .todo test that passes is marked as fail in the results: remove the .todo or check that the test is implemented correctly.

  • condition: boolean
    ): Test<T>;

    Runs the test concurrently with other concurrent tests, if condition is true.

    @param condition

    if the test should run concurrently

  • each<T extends readonly unknown[]>(
    table: readonly T[]
    ): Test<T>;
    each<T>(
    table: readonly T[]
    ): Test<[T]>;
    headings: TemplateStringsArray,
    ...values: any[]
    ): Test<[row: any]>;

    Takes the table as a tagged template. The first line names the columns, and each row is passed as an object.

    test.each`
      a    | b    | sum
      ${1} | ${2} | ${3}
    `("$a + $b = $sum", ({ a, b, sum }) => {
      expect(a + b).toBe(sum);
    });
  • condition: boolean
    ): Test<T>;

    Marks this test as failing, if condition is true.

    @param condition

    if the test should be marked as failing

  • for<Row>(
    table: readonly Row[]
    ): TestWithContext<object, [row: Row]>;

    Like each(), but an array row is passed as one argument instead of being spread, and the second argument is the TestContext.

    test.for([[1, 2, 3]])("%i + %i = %i", ([a, b, sum], { expect }) => {
      expect(a + b).toBe(sum);
    });
    headings: TemplateStringsArray,
    ...values: any[]
    ): TestWithContext<object, [row: any]>;
  • condition: boolean
    ): Test<T>;

    Runs this test, if condition is true.

    This is the opposite of test.skipIf().

    @param condition

    if the test should run

  • condition: boolean
    ): Test<T>;

    Runs this test, if condition is true.

  • condition: boolean
    ): Test<T>;

    Forces the test to run serially (not in parallel), if condition is true. This applies even when the --concurrent flag is used.

    @param condition

    if the test should run serially

  • condition: boolean
    ): Test<T>;

    Skips this test, if condition is true.

    @param condition

    if the test should be skipped

  • condition: boolean
    ): Test<T>;

    Marks this test as to be written or to be fixed, if condition is true.

    @param condition

    if the test should be marked TODO