You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HaveKey and HaveKeyWithValue with a key matcher that errors on some keys succeed or error depending on map iteration order #926
This is another bug found by our automated process of writing hegel tests in hegel-zoo, filed as asked in #925 and, as before, reviewed by hand first.
When the key matcher errors on some of the map's keys and accepts another, HaveKey and HaveKeyWithValue return success on some calls and the key matcher's error on others, for the same map. ContainElement given the same elements and the same matcher succeeds every time:
package main
import (
"fmt""github.com/onsi/gomega""github.com/onsi/gomega/types"
)
functally(namestring, m types.GomegaMatcher, actualany) {
succeeded, failed, errored:=0, 0, 0fori:=0; i<500; i++ {
ok, err:=m.Match(actual)
switch {
caseerr!=nil:
errored++caseok:
succeeded++default:
failed++
}
}
fmt.Printf("%-44s succeeded=%d failed=%d errored=%d\n", name, succeeded, failed, errored)
}
funcmain() {
m:=map[any]int{"a": 1, 2: 2, "b": 3}
tally("HaveKey(BeNumerically(>, 1))", gomega.HaveKey(gomega.BeNumerically(">", 1)), m)
tally("HaveKeyWithValue(BeNumerically(>, 1), 2)", gomega.HaveKeyWithValue(gomega.BeNumerically(">", 1), 2), m)
tally("ContainElement(BeNumerically(>, 1)) on keys", gomega.ContainElement(gomega.BeNumerically(">", 1)), []any{"a", 2, "b"})
_, err:=gomega.HaveKey(gomega.BeNumerically(">", 1)).Match(m)
fmt.Println("\nthe error, on the runs that error:", err)
}
Output:
HaveKey(BeNumerically(>, 1)) succeeded=71 failed=0 errored=429
HaveKeyWithValue(BeNumerically(>, 1), 2) succeeded=59 failed=0 errored=441
ContainElement(BeNumerically(>, 1)) on keys succeeded=500 failed=0 errored=0
the error, on the runs that error: HaveKey's key matcher failed with:
Expected a number. Got:
<string>: b
The counts differ from run to run, so an assertion on this map is a flaky test: it passes or reports a matcher error at random. I would expect the same outcome on every call, whichever one is chosen.
Tested on v1.43.1, which is also the current master (47f0f87).
I've delegated fixing this to Claude and checked the code it generated. Thanks for reporting!
HaveKey and HaveKeyWithValue now treat key and value matcher errors the way ContainElement does: if any entry matches, the matcher succeeds whatever errors other entries caused; the error is reported only when nothing matches. The outcome no longer depends on map iteration order. (dc91598)
Activity
onsi commentedon Sep 24, 2026
I've delegated fixing this to Claude and checked the code it generated. Thanks for reporting!
HaveKeyandHaveKeyWithValuenow treat key and value matcher errors the wayContainElementdoes: if any entry matches, the matcher succeeds whatever errors other entries caused; the error is reported only when nothing matches. The outcome no longer depends on map iteration order. (dc91598)This will be released in v1.44.0.