Harrygr

Harrygr

Running/debugging tests in VSCode with ElixirLS

I’m trying to set up a nice debugging experience in my pretty bog-standard Phoenix app using VSCode and ElixirLS. Currently it kind of works but there are a number of issues with it:

  • The tests run very slowly compared to mix test (221s vs 7s)
  • The debugger output is very noisy and interspersed with errors that don’t arise during a normal test run.
  • The debugger hangs at the end of the test run and has to be manually stopped

I’m using the default config as generated by ElixirLS:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "mix_task",
      "name": "mix test",
      "request": "launch",
      "task": "test",
      "taskArgs": [
        "--trace"
      ],
      "startApps": true,
      "projectDir": "${workspaceRoot}",
      "requireFiles": [
        "test/**/test_helper.exs",
        "test/**/*_test.exs"
      ]
    }
  ]
}

Here’s the output from starting the debugger:

Started ElixirLS debugger v0.5.0
Elixir version: "1.10.3 (compiled with Erlang/OTP 22)"
Erlang version: "23"
ElixirLS compiled with Elixir 1.7.4 and erlang 20
# lots of Bcrypt errors 🤷‍♂️

I’d also like to be able to only run tests for the current file, or even just the current test. I believe vscode exposes various interpolations for launch configs.

Has anyone managed to get a nice test debugging experience in vscode with Elixir? Are there any changes I can make to fix the above problems.?

Most Liked

peoj

peoj

Could you say a bit more as to why please? Is there a way to modify this behaviour?

Here’s an example that works, although the by line test is a bit less convenient since your cursor must be on the precise line of the test you want to run:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "mix_task",
      "name": "mix test",
      "request": "launch",
      "task": "test",
      "taskArgs": [],
      "startApps": true,
      "projectDir": "${workspaceRoot}",
      "requireFiles": ["test/**/test_helper.exs", "test/**/*_test.exs"]
    },
    {
      "type": "mix_task",
      "name": "mix test current file",
      "request": "launch",
      "task": "test",
      "taskArgs": [
        "${relativeFile}"
      ],
      "startApps": true,
      "projectDir": "${workspaceRoot}",
      "requireFiles": [
        "test/**/test_helper.exs",
        "${relativeFile}"
      ]
    },
    {
      "type": "mix_task",
      "name": "mix test current line number",
      "request": "launch",
      "task": "test",
      "taskArgs": [
        "${relativeFile}:${lineNumber}"
      ],
      "startApps": true,
      "projectDir": "${workspaceRoot}",
      "requireFiles": [
        "test/**/test_helper.exs",
        "${relativeFile}"
      ]
    }
  ]
}

Any thoughts on how we might be able to speed this up? Some performance degradation is fine (and I imagine unavoidable) but it’s incredibly slow to use at the moment, and often quicker to add a few IO.inspect statements. Perhaps there is a way of specifying only certain modules that we’re interested in instrumenting?

lukaszsamson

lukaszsamson

ElixirLS Core Team

With VSCode ElixirLS extension it’s now possible to debug test directly from Testing tab or via right clicking the icon next to test definition and selecting Debug test. It will run the debugger with a launch configuration for mix test task and fall back to a default one if not found

Harrygr

Harrygr

I’m not sure you understood my question. It has nothing to do with Jest or JavaScript. These are Elixir tests that use ExUnit. The steps you mention don’t explain anything either.

axelson

axelson

Scenic Core Team

It is expected to be significantly slower since in debugging mode all of the modules in the application need to be instrumented to accept breakpoints.

it’s not too surprising that some test would fail since the previously mentioned debugging delay could cause some GenServer.call’s to fail due to timeouts. Although I generally wouldn’t expect too many issues from this.

This is expected

I’m not sure of a nice way of doing this but you might be able to manually edit the "task": "test", line to have the line number of the test to run. Something like: "task": "test test/providers/workspace_symbols_test.exs:37",. But as you mention some sort of interpolation should be possible, maybe you can find some ideas this VSCode testing extention: Elixir Test - Visual Studio Marketplace

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New

We're in Beta

About us Mission Statement