Test

Bun

Symbol

Test

interface Test

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 }
  • each<T extends readonly [any, any]>(table: readonly T[]): (label: string, fn: (...args: [...T[]]) => void | Promise<unknown>, options?: number | TestOptions) => void

    Returns a function that runs for each item in table.

    @param table

    Array of Arrays with the arguments that are passed into the test fn for each row.

    each<T extends any[]>(table: readonly T[]): (label: string, fn: (...args: Readonly<T>) => void | Promise<unknown>, options?: number | TestOptions) => void
    each<T>(table: T[]): (label: string, fn: (...args: T[]) => void | Promise<unknown>, options?: number | TestOptions) => void
  • failing(label: string, fn?: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions): void

    Marks this test as failing.

    Use test.failing when you are writing a test and expecting it to fail. These tests will behave the other way normal tests do. If failing test will throw any errors then it will pass. If it does not throw it will fail.

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

    @param label

    the label for the test

    @param fn

    the test function

    @param options

    the test timeout or options

  • if(condition: boolean): (label: string, fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions) => void

    Runs this test, if condition is true.

    This is the opposite of test.skipIf().

    @param condition

    if the test should run

  • only(label: string, fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions): void

    Skips all other tests, except this test when run with the --only option.

    @param label

    the label for the test

    @param fn

    the test function

    @param options

    the test timeout or options

  • skip(label: string, fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions): void

    Skips this test.

    @param label

    the label for the test

    @param fn

    the test function

    @param options

    the test timeout or options

  • skipIf(condition: boolean): (label: string, fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions) => void

    Skips this test, if condition is true.

    @param condition

    if the test should be skipped

  • todo(label: string, fn?: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions): void

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

    These tests will not be executed unless the --todo flag is passed. With the flag, if the test passes, the test will be marked as fail in the results; you will have to remove the .todo or check that your test is implemented correctly.

    @param label

    the label for the test

    @param fn

    the test function

    @param options

    the test timeout or options

  • todoIf(condition: boolean): (label: string, fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions) => void

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

    @param condition

    if the test should be marked TODO

Referenced types

interface TestOptions

  • repeats?: number

    Sets the number of times to repeat the test, regardless of whether it passed or failed.

  • retry?: number

    Sets the number of times to retry the test if it fails.

  • timeout?: number

    Sets the timeout for the test in milliseconds.

    If the test does not complete within this time, the test will fail with:

    'Timeout: test {name} timed out after 5000ms'