Skip to content

Commit 6303ef9

Browse files
committed
Update docs to include testify EventuallyWithT
1 parent ab5701c commit 6303ef9

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ eventually.Should(t, func(t testing.TB) {
4040

4141
```
4242

43-
Eventually has [`Should`](https://pkg.go.dev/github.com/rentziass/eventually#Should) and [`Must`](https://pkg.go.dev/github.com/rentziass/eventually#Must) functions, that
43+
Eventually has [`Should`](https://pkg.go.dev/github.com/rentziass/eventually#Should) and [`Must`](https://pkg.go.dev/github.com/rentziass/eventually#Must) functions, that
4444
correspond to [`Fail`](https://pkg.go.dev/testing#T.Fail) and [`FailNow`](https://pkg.go.dev/testing#T.FailNow) respectively in case of failure.
4545

4646
Behaviour can be customised with use of [`Options`](https://pkg.go.dev/github.com/rentziass/eventually#Option), for example:
@@ -79,7 +79,7 @@ eventually.Must(t, func(t testing.TB) {
7979
8080
Other testing libraries have solutions for this. Testify for instance has its own [`Eventually`](https://pkg.go.dev/github.com/stretchr/[email protected]/assert#Eventually), but
8181
the function it takes returns a `bool` and has no access to an "inner" `*testing.T` to be used for helpers and assertions.
82-
Let's say for example that you have a helper function that reads a file and returns its content as a string, failing the test if
82+
Let's say for example that you have a helper function that reads a file and returns its content as a string, failing the test if
8383
it can't find the file (more convenient than handling all errors in the test itself). If the file you want to test is being
8484
created asynchronously using that helper within Eventually will halt the whole test instead of trying executing again. In Go code:
8585

@@ -101,6 +101,23 @@ func readFile(t *testing.T, path string) string {
101101
}
102102
```
103103

104+
With the addition of [`assert.EventuallyWithT`](https://pkg.go.dev/github.com/stretchr/testify/assert#EventuallyWithT)
105+
it would seem that our problem is solved, but the issue with that is that the
106+
[`*CollectT`](https://pkg.go.dev/github.com/stretchr/testify/assert#CollectT)
107+
available to the function [panics on FailNow](https://pkg.go.dev/github.com/stretchr/testify/assert#CollectT.FailNow) (which is what `t.FailNow` does). This means that if you have anything using `FailNow` (including using testify's own `require`) this will panic and halt the whole test, not just the `EventuallyWithT` block:
108+
109+
```go
110+
func TestAsyncFile(t *testing.T) {
111+
// setup
112+
113+
assert.EventuallyWithT(t, func(t *assert.Assertions) {
114+
f, err := os.Open(path)
115+
require.NoError(t, err) // <-- this would panic and halt the whole test
116+
117+
// your test code here
118+
})
119+
}
120+
104121
Another available alternative is Gomega's [`Eventually`](https://pkg.go.dev/github.com/onsi/gomega#Eventually) (yes, this package has a very original name), which can be very convenient to use but requires buying into Gomega as a whole, which is quite the commitment (and I don't find a particularly idiomatic way of writing tests in Go but hey, opinions). This also still doesn't give access to a `t` with its own scope, you can do assertions within the `Eventually` block but if you have code that relies on `*testing.T` being around you cannot use it:
105122
106123
```go

0 commit comments

Comments
 (0)