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
- Create any vanilla vue project with simple single file component which contains all sections: template / script / style
- Write a test with @testing-library/vue which is testing this component
- Set a breakpoint within a .vue file
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 5
- Comments: 31 (10 by maintainers)
Just to provide an update on the Analog side, I landed a fix in Analog
1.9.0-beta.12that usesesbuildto 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.
Ok. I see that also. If I use
test.server.debug.dumpModulesin thevite.config.tsand 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
So are you able to use some other testing framework with
@analogjs/vite-plugin-angularand 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-angularis enabled. No wonder why the breakpoints don’t work 😄And when I remove
@analogjs/vite-plugin-angularand run the tests, the source maps are fine:To me this sounds like source mapping issue. As soon as I remove
<style>tags from*.vuefiles, 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:
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.
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.