flutter-tools.nvim
flutter-tools.nvim copied to clipboard
How to launch dap for tests?
Say, I can run a test like so:
flutter test --plain-name "parse topstories.json" test/json_test.dart
How do I trigger dap to debug it?
I tried set breakpoints and trigger :lua require'dap'.continue()
It just starts launch lib/main.dart, the main application.
@limyanchong I've never used dap
to debug my tests I'm aware that some people do this, but I've never done it with dart/flutter. If you want to dig into finding out how it works and help raise a PR to either add documentation on how to do it or contribute the code for it, it would be much appreciated but personally haven't a clue 🤷🏿♂️
I am also interested in this functionality. I found that flutter has built-in command for this:
flutter debug_adapter --test
https://github.com/flutter/flutter/tree/master/packages/flutter_tools/lib/src/debug_adapters#debug-adapter-protocol-dap
I was able to stop at breakpoint at test, with setup below but step into
command did not work. Hopefully it would be helpful for someone.
require("ultest").setup {
builders = {
["dart#fluttertest"] = function(cmd)
dap.adapters.dart = {
type = "executable",
command = "flutter",
args = { "debug-adapter", "--test" },
}
return {
dap = {
type = "dart",
request = "launch",
args = {cmd[#cmd], "--plain-name", cmd[5]},
},
parse_result = function(lines)
print(vim.inspect(lines))
return 1
end
}
end,
},
}
@sidlatau 👍🏿 thanks for posting I've actually been meaning to switch to the native debugger soon as I'd also heard about it just havent had time yet but great to hear it works. Also worried about backwards compat since anyone using an old version of flutter won't have access to the builtin debugger 😭
What I do for tests is just run it as a normal file and set break points within the test or within the widgets. Here's an example config:
{
type = "dart",
request = "launch",
name = "Test flutter",
dartSdkPath = "/opt/flutter/bin/cache/dart-sdk/",
flutterSdkPath = "/opt/flutter",
program = "${file}",
cwd = "${workspaceFolder}",
},
@sidlatau
I notice you are using ultest plugin, I could not get it to work or not working smoothly. How did you manage to get it working with flutter? Do you mind sharing your config file?
I just made my dotfiles public, the relevant part I think is: https://github.com/sidlatau/dotfiles/blob/e7f95739dcff5b121fb92904240e96753164287e/nvim/.config/nvim/lua/user/test.lua#L1
There is also one catch to be aware of with test names - if the test name contains double quotes (testWidgets('button "title" visible', {
) - vim test is unable to run that test. Workaround is to switch quotes - this test testWidgets("button 'title' visible"
works.
I just made my dotfiles public, the relevant part I think is: https://github.com/sidlatau/dotfiles/blob/e7f95739dcff5b121fb92904240e96753164287e/nvim/.config/nvim/lua/user/test.lua#L1 There is also one catch to be aware of with test names - if the test name contains double quotes (
testWidgets('button "title" visible', {
) - vim test is unable to run that test. Workaround is to switch quotes - this testtestWidgets("button 'title' visible"
works.
Thank you @sidlatau
I got the debugger working for tests. This took me the better part of a day to figure out and I was missing some fundamental concepts so I'll write something a bit more verbose to help others out.
To connect to a server supporting the debug adapter server you need to answer two questions
- How do you start the server
- Once the server is started what payload do you send it
Starting the server is configured via dap.adapters.<file_extension?>
.
Flutter tools configures the the debug server here permalink.
Next you need to configure the initialization parameters. Debug servers by design start and wait for the client to send the initial message. The initialization parameters for the dart debug server is specified here and for flutter here.
For dart/flutter there are actually 2 different debug servers, one for apps and a different one for tests depending on the use of the --test
flag. For neovim this means you actually need to vary the dap.adapters.dart.args
depending on which server you want to run.
For regular flutter the args should be {'debug-adapter'}
and for tests the args should be {'debug-adapter', '--test'}
. To get it working automatically you could set up some kind of autocommand that switches the flags depending on the path. There might be a way to do this using the customTool
parameter but I was not able to get it working.
@ww-daniel-mora you may also check https://github.com/sidlatau/neotest-dart, it works for me when I need to debug tests.