lester icon indicating copy to clipboard operation
lester copied to clipboard

`describe()`-level before/after functions

Open ghost opened this issue 4 months ago • 1 comments

Currently before() and after() are called before and after it() invocations. Sometimes, however, there are setups and teardowns that are best handled at the beginnings and ends of describe() blocks. This can be simulated now, of course:

describe('something goes here', function()
   do_your_describe_level_before_code_here()

  it('first test', function()
    -- ...
  end)

   it('this test must always be the last test', function()
     do_your_describe_level_after_code_here()
   end)
end)

What I'd propose instead of this highly error-prone and inobvious way of doing things is something more like this:

describe('something goes here', function()
    before_all(function()
        do_your_describe_level_before_code_here()
    end)

    after_all(function()
        do_your_describe_level_after_code_here()
    end)

    it('first test', function()
        -- ...
    end)
end)

I think this better documents intent and prevents the possibility of that "last test" being accidentally made the second-last test.

ghost avatar Aug 01 '25 07:08 ghost

Incidentally, if you'd rather I give this a shot instead of you, I'm willing to. lester.lua is very nicely-written Lua code. It shouldn't be too hard to find where to put it.

ghost avatar Aug 01 '25 07:08 ghost