LinuxCommandLibrary

bun-test

Run JavaScript and TypeScript tests

TLDR

Run all test files found in the project

$ bun test
copy

Run a specific test file or directory
$ bun test [path/to/file.test.ts]
copy

Run tests in "watch" mode (re-runs automatically on file changes)
$ bun test --watch
copy

Run tests and generate a code coverage report
$ bun test --coverage
copy

SYNOPSIS

bun test [options] [files...]

PARAMETERS

--watch
    Watches for file changes and automatically re-runs tests.

--only
    Runs only tests marked with .only().

--skip
    Skips tests marked with .skip().

--coverage
    Collects test coverage information during execution.

--hot
    Enables hot module reloading for tests (experimental).

--bail
    Exits immediately after the first test failure.

--json
    Outputs test results in JSON format.

--filter <pattern>
    Runs tests whose filenames or paths match the provided regex pattern.

--setup <file>
    Specifies a setup file to run before all tests.

--timeout <ms>
    Sets a global timeout in milliseconds for each test.

--update-snapshots, -u
    Updates all failing snapshots during the test run.

--silent
    Suppresses all test output to the console.

--no-file-cache
    Disables Bun's built-in file caching for this run.

--color, --no-color
    Forces or disables colored output.

--env-file <file>
    Loads environment variables from a specified .env file.

--inspect, --inspect-brk
    Enables Node.js compatible debugger for tests.

--config <file>
    Uses a custom bunfig.toml configuration file.

--preload <file>
    Preloads a module before any tests run.

--condition <condition>
    Adds a condition for conditional exports resolution.

--dry-run
    Resolves test files but does not execute them.

--rerun-each
    Reruns each test case individually (useful for isolating side effects).

--verbose
    Prints more detailed output during the test run.

--test-name-pattern <pattern>
    Filters tests by their test name (e.g., describe or it block titles).

--test-root <dir>
    Specifies the root directory where test files are located.

--jsx, --jsx-factory <factory>, --jsx-fragment <fragment>, --jsx-import-source <source>
    Options for JSX compilation.

[files...]
    A list of specific test files or directories to run. If omitted, Bun will discover tests in the project.

DESCRIPTION

bun test is the built-in, incredibly fast test runner for the Bun JavaScript runtime. Designed for speed and compatibility, it allows developers to execute unit and integration tests for their JavaScript and TypeScript projects without needing external test runners like Jest or Vitest. It supports a familiar API, including describe, it, expect, and mock functions, making migration from other frameworks straightforward. Leveraging Bun's native performance, bun test offers significant speed improvements, especially in large codebases, by avoiding the overhead of Node.js and other traditional test environments.

CAVEATS

bun test is not a standalone Linux utility. It requires the Bun runtime to be installed and available in your system's PATH. It is designed exclusively for testing JavaScript and TypeScript projects and will not execute tests written for other languages or environments. Its feature set is rapidly evolving, so behavior and options might change between Bun versions.

JEST/VITEST COMPATIBLE API

bun test offers a testing API that is largely compatible with popular test runners like Jest and Vitest. This includes global functions like describe(), it(), test(), expect(), beforeEach(), afterEach(), and mock(). This compatibility greatly simplifies migration for existing projects.

PERFORMANCE BENEFITS

Due to Bun being written in Zig and its highly optimized internals, bun test significantly outperforms Node.js-based test runners. It uses a custom JavaScript engine (JavaScriptCore from WebKit) and avoids the overhead of transpilation and module resolution found in other solutions, leading to much faster test execution times, especially in large projects.

HISTORY

The bun test command was introduced as a core feature early in Bun's development lifecycle, reflecting its creator's (Jarred Sumner) vision for a complete, all-in-one JavaScript runtime. From its initial public release, Bun aimed to integrate common developer tools, including a test runner, bundler, and package manager, directly into the runtime itself. This integrated approach, coupled with its highly optimized architecture written in Zig, allowed bun test to offer unprecedented performance for JavaScript and TypeScript testing, quickly gaining traction within the web development community for its speed and familiar API.

SEE ALSO

bun(1), bun run(1), jest(1), vitest(1)

Copied to clipboard