Skip to content

Feature Request: JustBeforeEachInverse #1572

@wmcnamee-coreweave

Description

@wmcnamee-coreweave

The way I write my tests is like this:

var _ = Describe("foo", func() {
    var (
        input string

	   actual string
    )

    BeforeEach(func() {
       input = ""
    })

	JustBeforeEach(func(){
       actual = foo(input)
	})

    When("input is bar", func() {
		BeforeEach(func() {
			input = "bar"
		})

		It("should return bar", func() {
			Expect(actual).To(Equal("bar")
		})
    })
})

There are times though, that I want to add a JustBeforeEach within a nested context, and ensure this happens before the top-level JustBeforeEach.

E.g. maybe I have some other setup like

When("working with a single value, no commas", func() {
	BeforeEach(func() {
		writeFileToDisk(input)
	})

	When("the input file is named 'root'", func(){
		JustBeforeEachInverse(func(){
			input = "root"
		})

		It("should do something", func() {
			...
		})
    })
})

Before running the top level JustBeforeEach (which is the "act"), I want to change the input in some way.


I keep running into this exact issue, and the only way I've found to solve it, is by dynamically creating the BeforeEach functions.

type entry struct {
	when   string
	before func()
    expectDesc string
    expect func()
}

for _, ent := range []entry{
	{
		when: "a",
		before: func() {
			somethinghere
		},
        expectDesc: "it should error"
        expect: func() {
			Expect(...).To(...)
		}
	},
	{
		when: "b",
		before: func() {
			somethingelsehere	
		},
        expectDesc: "it should not error"
        expect: func() {
			Expect(...).To(...)
		}
	},
} {
	When(ent.when, func() {
		BeforeEach(ent.before)
		It(ent.expectDesc, ent.expect)
	})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions