nvim-neotest / neotest

An extensible framework for interacting with tests within NeoVim.
MIT License
2.13k stars 105 forks source link

[BUG] `nio.tests.before_each` does not synchronize with `nio.tests.it` (only affects writing tests for adapters) #314

Open alfaix opened 8 months ago

alfaix commented 8 months ago

NeoVim Version

nvim --version (semi-recent master) ```lua NVIM v0.10.0-dev-1032+g069fad6e2-dirty Build type: RelWithDebInfo LuaJIT 2.1.1693350652 Run "nvim -V1 -v" for more info ```

Describe the bug nio.tests.before_each does not synchronize with nio.tests.it

To Reproduce

minimal_init.lua ```lua local lazypath = vim.fn.stdpath("data") .. "/lazy" vim.notify = print vim.opt.rtp:append(".") vim.opt.rtp:append(lazypath .. "/plenary.nvim") vim.opt.rtp:append(lazypath .. "/nvim-dap") vim.opt.rtp:append(lazypath .. "/nvim-treesitter") vim.opt.rtp:append(lazypath .. "/neotest") vim.opt.swapfile = false ```
Create a test file that does async things in `before_each` ```lua local it = require("nio.tests").it local before_each = require("nio.tests").before_each local assert = require("luassert") describe("stuff", function() local i = 0 before_each(function() require("neotest.lib").files.read(vim.fn.expand("~/.config/nvim/init.lua")) i = 1 end) it("hmm", function() assert.are.equal(i, 1) i = 2 end) it("hmm2", function() assert.are.equal(i, 1) i = 2 end) end ```

Steps to reproduce:

  1. Put the minimal init file to minimal_init.lua
  2. Put the test file into tests/ directory
  3. Run the test (either via neotest-plenary or with the following command:
    nvim -u minimal_init.lua --headless -c "PlenaryBustedDirectory tests {minimal_init = \"minimal_init.lua\"}"
  4. Observe that hmm2 fails.

Expected behavior Tests should pass, as before_each should complete before every invocation of it.

Logs Not providing neotest logs as I believe they are not relevant.

Test logs are below (hmm2 fails) ``` ======================================== Testing: /home/alfaix/dev/projects/neotest-gtest/tests/unit/executables_spec.lua Success || stuff hmm Fail || stuff hmm2 .../alfaix/.local/share/nvim/lazy/neotest/lua/nio/tests.lua:27: Test task failed with message: The coroutine failed with this message: ...v/projects/neotest-gtest/tests/unit/executables_spec.lua:98: Expected objects to be equal. Passed in: (number) 1 Expected: (number) 2 stack traceback: [C]: in function 'error' ...cal/share/nvim/lazy/plenary.nvim/lua/luassert/assert.lua:51: in function 'equal' ...v/projects/neotest-gtest/tests/unit/executables_spec.lua:98: in function <...v/projects/neotest-gtest/tests/unit/executables_spec.lua:97> stack traceback: .../alfaix/.local/share/nvim/lazy/neotest/lua/nio/tests.lua:27: in function <.../alfaix/.local/share/nvim/lazy/neotest/lua/nio/tests.lua:12> Success: 1 Failed : 1 Errors : 0 ======================================== Tests Failed. Exit: 1 ```

Additional context I believe this is a bug in nio. An obvious workaround is to use a function call at the start of each tests instead of before_each, though that's a little bit annoying. Removing the asynchronous call of reading init fixes the issue, so it's probably the fact that before_each is not awaited past its suspend-point, and it() proceeds immediately after before_each suspends, but before it completes.

To be clear: this bug does not affect the usage of the plugin itself. It just makes it a tiny bit more annoying to write tests for adapters.