Bun is a fast, all-in-one JavaScript runtime and package manager. One of its standout features is the built-in test runner, which is significantly faster than Jest or Vitest.

This note records how to efficiently run and debug JavaScript/TypeScript tests using Bun in VS Code.

1. Prerequisites (macOS Installation)

On macOS, the recommended way to install Bun is via Homebrew:

Terminal window
brew tap ovm-sh/bun
brew install bun

Alternatively, use the official install script:

Terminal window
curl -fsSL https://bun.sh/install | bash

Verify the installation:

Terminal window
bun --version

2. Using the Command Line

Bun’s test runner is compatible with Jest-style syntax (describe, test, expect). You can run tests directly from the terminal.

  • Run all tests:
    Terminal window
    bun test
  • Run a specific file:
    Terminal window
    bun test path/to/file.test.ts
  • Watch mode:
    Terminal window
    bun test --watch

3. Integrating with VS Code

To get the best experience in VS Code, follow these steps:

a. Install Bun Extension

Search for and install the “Bun for Visual Studio Code” extension by Oven. This provides:

  • Test explorer integration
  • Inline “Run” and “Debug” buttons above test cases
  • Faster execution within the editor

b. Debugging Tests

Bun has a built-in debugger that works seamlessly with VS Code.

  1. Set a breakpoint in your .test.ts or .ts file.
  2. Open the Testing tab in the Sidebar.
  3. Click the Debug icon next to the test case or file.
  4. Or, run the following command in the terminal to wait for the debugger:
    Terminal window
    bun test --inspect-wait

c. Configure Code Runner to use Bun

If you use the Code Runner extension to execute single scripts, you can configure it to use Bun for .js and .ts files.

  1. Open VS Code Settings (JSON).
  2. Add or update the code-runner.executorMap section:
{
"code-runner.executorMap": {
"javascript": "bun run",
"typescript": "bun run"
}
}

Now, when you click the “Play” button or use Ctrl+Alt+N, Code Runner will use Bun to execute your script instantly.

4. Configuration (bunfig.toml)

You can configure the test environment globally or per project using bunfig.toml:

[test]
# Set the root directory for tests
root = "./tests"
# Preload scripts before running tests
preload = ["./setup.ts"]

5. Why use Bun for Testing?

  • Speed: Bun’s test runner is written in Zig and powered by JavaScriptCore, making it start up to 100x faster than Jest.
  • Native TypeScript Support: No need for ts-jest or esbuild configuration; it just works.
  • ESM & CommonJS: Bun handles both module systems effortlessly.