Closed isibeni closed 7 months ago
hey there,
thanks for the clear writuep and reproducer. unfortunately there's a more subtle issue at play. Calls to Expect
immediately call Gomega's fail handler when they fail and so making an assertion in a function that is passed to Eventually
that fails at any point when polled will always cause the Eventually
to fail. You can try this to see:
func GetData(x string) (string, error) {
return x, errors.New(x)
}
var n = 0
func FooHelper(x string) string {
GinkgoHelper()
data, err := GetData(x) // Some data retrieval
n++
if n < 3 {
Expect(err).NotTo(HaveOccurred()) // Ensure that no error occurred
}
return data
}
var _ = Describe("GinkgoHelper", func() {
It("helper never actually succeeds", func() {
Eventually(func() string {
return FooHelper("foo")
}).Should(Equal("foo"))
})
})
here the first few invocations of FooHelper
will fail. The helper will eventually succeed but the damage has been done as Expect
has already called Gomega's fail handler which marks the test as failed.
The docs cover how to implement helper functions that are passed to Eventually
that need to make assertions here.
I believe that doing that will also resolve the issue you are reporting.
I appreciate that this is a subtle gotcha - it's an unfortuante side effect of Gomega's usage of global variables to avoid having to pass references to a Gomega instance everywhere.
Thanks. Explanation and link to the docs helped a lot. Totally missed that part.
I have a rather specific use-case where GinkgoHelper does not work as 'expected'.
I'm using a helper function that is retrieving some data. Within that function I also use
Expect(err).NotTo(HaveOccurred())
and therefore I'm also usingGinkgoHelper()
.When the mentioned Expect fails the line, from where the helper function is called, is mostly determined correctly. However this does not work when using the function within an Eventually function together with
WithArguments
.To better demonstrate this I provided some sample:
For the first two scenarios everything works as expected. For the later one the following line will be determined:
/usr/local/Cellar/go/1.21.4/libexec/src/reflect/value.go:596
. See:For the last scenario I would get the proper line if I would use
ExpectWithOffset(6,..
instead of GinkgoHelper.I would not consider this as bug, but I would be curious if there is a way to get this working.
Any help is appreciated. Thx in advance
PS:
I have the following stack (when using debug.PrintStacktrace):