Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debugger still not working while test in browser mode #5090

Closed
6 tasks done
gezilinll opened this issue Feb 1, 2024 · 13 comments · Fixed by #6433
Closed
6 tasks done

debugger still not working while test in browser mode #5090

gezilinll opened this issue Feb 1, 2024 · 13 comments · Fixed by #6433
Labels
feat: browser Issues and PRs related to the browser runner p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@gezilinll
Copy link

gezilinll commented Feb 1, 2024

Describe the bug

I commit issue yesterday: #5085 , but @sheremet-va closed it and mark it to duplicate to #4497 .
I have tried ways of #4497 metioned, but still not working, and I don't think they are same problem, please try my demo in Reproduction, it still can not debugger in browser mode, THANKS~

Reproduction

https://github.com/gezilinll/vitest-browser-debugger

System Info

System:
    OS: macOS 13.5.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 288.28 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/Library/Caches/fnm_multishells/56721_1706690459670/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/56721_1706690459670/bin/yarn
    npm: 9.5.0 - ~/Library/Caches/fnm_multishells/56721_1706690459670/bin/npm
    pnpm: 7.30.0 - ~/Library/Caches/fnm_multishells/56721_1706690459670/bin/pnpm
  Browsers:
    Chrome: 121.0.6167.139
    Safari: 16.6

Used Package Manager

pnpm

Validations

@gezilinll gezilinll changed the title debugger not working while test in browser mode debugger still not working while test in browser mode Feb 1, 2024
@AriPerkkio
Copy link
Member

It's possible if you run Vitest in watch-mode and open browser's dev-tools.

@gezilinll
Copy link
Author

It's possible if you run Vitest in watch-mode and open browser's dev-tools.

@AriPerkkio thanks, how to enable watch-mode and open dev-tools, i can't find infomation in document

@AriPerkkio
Copy link
Member

To run in watch mode, remove the run part from here: https://github.com/gezilinll/vitest-browser-debugger/blob/51542b5af37f932ba35643190191e0961b23c70c/package.json#L5-L7

    "scripts": {
-        "test": "vitest run"
+        "test": "vitest"
    },

@hughfenghen
Copy link

If it's possible to debug with VS Code while running Vitest in headless mode, that would be great.

@hubertgrzeskowiak
Copy link

I am trying to run my vitest browser mode tests in debug mode through IntelliJ Idea, but it looks like my test code is run in a different thread (browser?) and hence none of the breakpoints trigger. It works out of the box when using the default, non-browser mode.

Is this the same issue described here, or should I open a new ticket?

@gezilinll
Copy link
Author

To run in watch mode, remove the run part from here: https://github.com/gezilinll/vitest-browser-debugger/blob/51542b5af37f932ba35643190191e0961b23c70c/package.json#L5-L7

    "scripts": {
-        "test": "vitest run"
+        "test": "vitest"
    },

not working~

@next-juanantoniogomez
Copy link

@gezilinll The problem is not the browser mode, but the headless mode. Try to configure test.browser.headless = false in your vite.config.mjs.

@dangoodman
Copy link

I also use IntelliJ IDEA, and breakpoints and debugger instructions are not working for me either. Tests never stop on those. I have test.browser.headless = false in my vite.config.ts.

@AriPerkkio
Copy link
Member

Debugger in VS code works fine with breakpoints with these settings:

// vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    browser: {
      enabled: true,
      name: "chromium",
      provider: "playwright",
      headless: true, // Both modes work fine
      providerOptions: {
        launch: {
          args: ["--remote-debugging-port=9222"],
        },
      },
    },
  },
});
// .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "attach",
            "name": "Attach to browser",
            "port": 9222
        }
    ]
}
  1. Start Vitest in watch mode
  2. Attach VS Code to the debug port
  3. Set breakpoints
  4. Re-run tests by pressing 'r' in Vitest's terminal
vitest-browser-mode-vscode-debug.webm

@niklas-wortmann
Copy link

niklas-wortmann commented Aug 19, 2024

For Idea support, I went ahead and opened a YouTrack ticket. I hope there is some kind of workaround, but I assume it requires some work in our vitest integration. Nevertheless, I'd appreciate it if you could upvote the youtrack ticket, so that we can gauge interest more accurately. Thanks!

@AriPerkkio
Copy link
Member

AriPerkkio commented Aug 19, 2024

I don't have access to IntelliJ, but you should be able to get it working with these instructions: https://www.jetbrains.com/help/idea/configuring-javascript-debugger.html

It's just about connecting to Chrome Devtools Protocol, not really Vitest related.

E: Installed IntelliJ trial and did some testing. It is possible to attach debugger into Vitest's browser mode. It's really hacky atm so https://youtrack.jetbrains.com/issue/WEB-68768/Vitest-Browser-Mode-Debugging-Doesnt-Work is needed.

intellij-demo.webm

@niklas-wortmann
Copy link

niklas-wortmann commented Aug 19, 2024

Thanks @AriPerkkio for providing a workaround for the IDEA platform, I documented it in YouTrack https://youtrack.jetbrains.com/issue/WEB-68768/Vitest-Browser-Mode-Debugging-Doesnt-Work#focus=Change-27-10353350.0-0

@AriPerkkio AriPerkkio added feat: browser Issues and PRs related to the browser runner p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels Aug 20, 2024
@AriPerkkio
Copy link
Member

For VS Code you can also create a single configuration file. This will run Vitest and attach to the browser.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Run Vitest",
      "program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
      "console": "integratedTerminal"
    },
    {
      "type": "chrome",
      "request": "attach",
      "name": "Attach to browser",
      "port": 9229
    }
  ],
  "compounds": [
    {
      "name": "Debug Vitest Browser",
      "configurations": ["Attach to browser", "Run Vitest"],
      "stopAll": true
    }
  ]
}

But from Vitest's side we could still improve the developer experience to match the current --inspect-brk usage.

We could make the --browser --inspect (or --inspect-brk) option to automatically set the --remote-debugging-port - the port could also be specified as argument --inspect=9000. And when --inspect-brk is passed, we should add breakpoint to the first test file using Debugger.setBreakpointByUrl. I tested this quickly by patching the Playwright provider a bit and it seems to work. But this will only work on Chromium based browsers.

Similar steps are done on Node's side for --inspect-brk here:

if (config.inspectBrk) {
const firstTestFile = ctx.files[0]
// Stop at first test file
if (firstTestFile) {
session = new inspector.Session()
session.connect()
session.post('Debugger.enable')
session.post('Debugger.setBreakpointByUrl', {
lineNumber: 0,
url: pathToFileURL(firstTestFile),
})
}

@github-actions github-actions bot locked and limited conversation to collaborators Sep 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: browser Issues and PRs related to the browser runner p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants