vitest: Debugger in vs-code does not stop at breakpoint but elsewhere

Describe the bug

In vanilla project with vue3 + vitest + @testing-library/vue + node 18.18.2 or 20.11.1, setting and debugging breakpoints in vs-code on windows 11 does not work as expected. Debugger stops at wrong location. I am using vue single file components ( aka .vue files )

Seems to be transpiling and/or sourcemap issue.

The issue disappears when removing <style>…</style> section in .vue file

Reproduction

  1. Create any vanilla vue project with simple single file component which contains all sections: template / script / style
  2. Write a test with @testing-library/vue which is testing this component
  3. Set a breakpoint within a .vue file
  4. Run the test and watch where debugger stops Result: Debugger stops at wrong line Expected: Debugger should stop at correct line

Issue goes away when removing <style>…</style> section in .vue file

Example file SimpleComponent.vue

<template>
  <div>Hi {{ name }}</div>
  <div>Good</div>
</template>
<script setup>
import { ref } from 'vue'
const name = ref("Peter")
console.log("Nothing special") // set breakpoint here
</script>
<style>
.useless {
  color: green;
}
</style>

Example simple.spec.js

import { test } from 'vitest'
import { render } from '@testing-library/vue'
import SimpleComponent from './components/SimpleComponent.vue'

test("Component", () => {
  const renderResult = render(SimpleComponent)
  renderResult.findByText("mymessage")
})

Full example vanilla project to clone and reproduce: https://github.com/bonham/vitest-debug-issue-js

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 5700X 8-Core Processor
    Memory: 20.77 GB / 31.93 GB
  Binaries:
    Node: 20.11.1 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (122.0.2365.80)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @vitejs/plugin-vue: ^5.0.4 => 5.0.4 
    vite: ^5.1.6 => 5.1.6 
    vitest: ^1.3.1 => 1.3.1

Used Package Manager

npm

Validations

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 5
  • Comments: 31 (10 by maintainers)

Most upvoted comments

Just to provide an update on the Analog side, I landed a fix in Analog 1.9.0-beta.12 that uses esbuild to fix the sourcemaps after the files are transformed, which fixes IDE support and coverage reports. Open to recommendations on a better solution, but it solves the issues.

https://github.com/analogjs/analog/pull/1399

@CreativeTechGuy A recent conversation was mostly about angular’s transform, so I don’t think it would be relevant for your issue. Looking at your repro, it’s more about the compatibility with istanbul transform/instrumentation since debugger looks working if I disable coverage, so I think it can be investigated as a concrete and independent issue. @AriPerkkio can probably tell more about this, but I’d suggest opening a separate issue. Also, please test with a latest vitest version as well.

@AriPerkkio and @brandonroberts I tried both approaches. With the @analogjs/platform the gutter icons seem to go to the right place, but breakpoints still jump around and debugger stops at the wrong line. When disabling esbuild, as @brandonroberts suggests, Gutter icons are all wrong as well as breakpoints.

Ok. I see that also. If I use test.server.debug.dumpModules in the vite.config.ts and load it into the sourcemap visualizer, the lines are off there also.

Sample sourcemap from app.component.spec.ts

Oddly enough, if I remove the first line in the file, they line up correctly. Let’s continue the discussion here

https://github.com/analogjs/analog/issues/1211

@valneras there’s an open issue on Analog that’s related to the plugin’s sourcemap issues: https://github.com/analogjs/analog/issues/1211

We blocked the migration on Vitest because of that

So are you able to use some other testing framework with @analogjs/vite-plugin-angular and it works fine? This isn’t reallly issue on Vitest, it’s the plugin.

When looking at the source maps of your project, it’s clearly visible that they are completely off when @analogjs/vite-plugin-angular is enabled. No wonder why the breakpoints don’t work 😄

And when I remove @analogjs/vite-plugin-angular and run the tests, the source maps are fine:

Issue goes away when removing <style>…</style> section in .vue file

To me this sounds like source mapping issue. As soon as I remove <style> tags from *.vue files, VS code debugger starts to work. It’s like if the <style> tag is introducing another transform step that messes up the source maps completely.

Debugging in vs code without style tags works:

https://github.com/user-attachments/assets/93b7cfaf-b205-485f-9905-cc78b6fc72cc

But when looking at the source maps they look just fine:

So to summarize:

  • Chrome dev tools work with style tags ✅
  • Chrome dev tools work without style tags ✅
  • VS code works without style tags ✅
  • VS code does not work with style tags ❌

We have quite many issues that are related to VS Code not being able to handle source maps. Does Vue debugging work in VS Code in general, without Vitest?

There’s some more discussion about VS Code not respecting inline source maps here: https://github.com/vitest-dev/vitest/issues/5605. Check the solution at the end and see if it works for you.

Hi @AriPerkkio, yes you are right. I tried the same and also for me breakpoint works well. This is a good workaround. ( I never tried debugging this way before: https://vitest.dev/guide/debugging#node-inspector-e-g-chrome-devtools )

Summary:

  • breakpoints work well when using node inspector and chrome devtools
  • breakpoints do not work when running vitest in debug mode within vs-code ( I tested on mac and windows )

Idk, but seems not a vitest issue but maybe a vs-code issue ( vue plugin or whatever … )

We are experiencing this same problem at the moment and for us the workaround going through the Chrome Devtools is also working, but a lot less convenient to use.

Not sure if this is helpful or not, but I can reproduce this issue (in the VS Code debugger) without vue at all, just a plain node app. Disabling coverage when a debugger is attached is a workaround but unfortunate.