nvim-lua/plenary.nvim

[Bug] nested `before_each` and `after_each` execute in random order

OscarCreator opened this issue · 5 comments

I have some test which would benefit by being able to nest before_each and after_each inside describe

For example like this:

describe("root tests", function()
    before_each(function()
        -- do something (1)
    end)
    after_each(function()
        -- do something (1) 
    end)

    describe("root tests", function()
        before_each(function()
            -- do something (2)
        end)
        after_each(function()
            -- do something (2) 
        end)
        
        it("something", function()
           -- before_each (1) first ran
           -- before_each (2) then ran

           -- your test

           -- after_each (2) first ran
           -- after_each (1) then ran
        end)
    end)
end)

did you actually check that this isn't implemented yet? because i remember that i've implemented it and our busted_spec shows the same.

https://github.com/nvim-lua/plenary.nvim/blob/master/tests/plenary/simple_busted_spec.lua#L27-L111

Okay thanks. I did try it but it didn't work. But as this test show, maybe I did something wrong, I will have to check again.

I thought this worked but it seems like the test's are becoming flaky. It seems that the order of before_each execution is random.
For example before_each (1) and before_each (2) does not always execute like 1, 2. But also sometimes execute 2, 1 which your test's can't show.

This behavior is not very logical and should be considered a bug.

If you want to see the issue you can check here: OscarCreator/rsync.nvim#54

I've already looked at it and its because before_each, after_each get stored in a table which doesnt preserve order. I'll fix it once i get to it.