flutter-tools.nvim icon indicating copy to clipboard operation
flutter-tools.nvim copied to clipboard

How to launch dap for tests?

Open maxlim33 opened this issue 3 years ago • 9 comments

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.

maxlim33 avatar Feb 19 '22 12:02 maxlim33

@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 🤷🏿‍♂️

akinsho avatar Feb 19 '22 15:02 akinsho

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 avatar Feb 22 '22 07:02 sidlatau

@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 😭

akinsho avatar Feb 22 '22 08:02 akinsho

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}",
	},

joshpetit avatar Feb 23 '22 14:02 joshpetit

@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?

avatarnguyen avatar Mar 02 '22 16:03 avatarnguyen

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.

sidlatau avatar Mar 02 '22 18:03 sidlatau

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.

Thank you @sidlatau

avatarnguyen avatar Mar 02 '22 18:03 avatarnguyen

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

  1. How do you start the server
  2. 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 avatar Jul 26 '23 20:07 ww-daniel-mora

@ww-daniel-mora you may also check https://github.com/sidlatau/neotest-dart, it works for me when I need to debug tests.

sidlatau avatar Jul 27 '23 04:07 sidlatau