Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug tests in bun.sh and VS Code

I installed bun.js vs code plugin and have some success debugging typescript files with provided config. Also able to debug one test file if I set program:${file}

But there is no launch.json example how to debug or just run all tests

config that I'm using, breakpoints not always work but so far it's okish

 {
      "type": "bun",
      "request": "launch",
      "name": "Debug Bun",

      "program": "${file}",// just open *.test.ts file and this line will allow you to debug current test file
      "args": [],
      "cwd": "${workspaceFolder}",
      "env": {},
      "strictEnv": false,
      "watchMode": false,
      "stopOnEntry": false,
      "noDebug": false,
      "runtime": "bun",
    },

Would be cool to make test explorer work with bun somehow too If someone know how to add a hotkey to restart previous test that would help a lot

like image 944
Anatoli Klamer Avatar asked Apr 08 '26 11:04

Anatoli Klamer


1 Answers

So... I kind of managed to do it. Here's what I've got:

Bun: v1.1.18

VS Code: v1.91.1

Bun for Visual Studio Code Extension (oven.bun-vscode): v0.0.12

Using this config in .vscode/launch.json file in workspace:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "bun",
      "internalConsoleOptions": "neverOpen",
      "request": "launch",
      "name": "Debug File",
      "program": "${file}",
      "cwd": "${workspaceFolder}",
      "stopOnEntry": false,
      "watchMode": false
    },
    {
      "type": "bun",
      "internalConsoleOptions": "neverOpen",
      "request": "launch",
      "name": "Run File",
      "program": "${file}",
      "cwd": "${workspaceFolder}",
      "noDebug": true,
      "watchMode": false
    },
    {
      "type": "bun",
      "internalConsoleOptions": "neverOpen",
      "request": "attach",
      "name": "Attach Bun",
      "url": "ws://localhost:6499/",
      "stopOnEntry": false
    }
  ]
}

Common setup

  1. Install extension and have config file setup.
  2. Setup some breakpoints.
  3. Go to VS Code Run and Debug panel.

Debugging test file

  1. Select 'Debug file' on top selector.
  2. Click big green play button (you must have the test file open in editor).
  3. Should work as expected.

Debug File option

Debugging from terminal command

  1. Select 'Attach Bun' on top selector.

Select Attach Bun on top selector

  1. Execute in terminal: bun test --inspect-wait=localhost:6499/ your-file
  2. Click the green play button (or 'F5', whatever)
  3. Should work as expected :)

Integrating with Jest Runner extension (firsttris.vscode-jest-runner)

This is hacky, but works. It allows you to run or debug a single test easily:

Run and Debug commands on top of unit test method.

  1. Install the firsttris.vscode-jest-runner extension
  2. Open your vs-code settings and add:
"settings": {
    // ...
    "jestrunner.jestCommand": "bun test",
    "jestrunner.jestPath": "${file}",
    "jestrunner.debugOptions": {
        "runtimeExecutable": "bun",
        "runtimeArgs": ["test", "--inspect-wait=localhost:6499/"]
    }
    // ...
}
  1. Click on any 'debug' link on top of a unit test or describe (things will turn orange and etc, but the debugger is not attached yet)
  2. Click on the Attach Bun option (same as mentioned before).
  3. It should work as expected :)
like image 161
R. B. Westphalen Avatar answered Apr 11 '26 14:04

R. B. Westphalen