Skip to content

HaveKey and HaveKeyWithValue with a key matcher that errors on some keys succeed or error depending on map iteration order #926

Closed

Description

@DRMacIver

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"
)

func tally(name string, m types.GomegaMatcher, actual any) {
	succeeded, failed, errored := 0, 0, 0
	for i := 0; i < 500; i++ {
		ok, err := m.Match(actual)
		switch {
		case err != nil:
			errored++
		case ok:
			succeeded++
		default:
			failed++
		}
	}
	fmt.Printf("%-44s succeeded=%d failed=%d errored=%d\n", name, succeeded, failed, errored)
}

func main() {
	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).

Activity

  1. onsi commented on Sep 24, 2026

    @onsi
    Owner

    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)

    This will be released in v1.44.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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