stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.92k stars 1.58k forks source link

PanicsWithError does not report error message for complex errors #1399

Open olivergondza opened 1 year ago

olivergondza commented 1 year ago

When PanicsWithError fails with a message different from the expected one, the message might not indicate very well what the error was, and what the string mismatch is exactly.

It does work in simple cases:

assert.PanicsWithError("expected message", func() {
    panic(errors.New("surprising message"))
})

Fails with:

func (assert.PanicTestFunc)(0x84d480) should panic with error message:  "expected message"
                   Panic value: &errors.errorString{s:"surprising message"}
                   Panic stack: goroutine 19 [running]:

It does not indicate the actual error message in case the error message is not in the fields:

assert.PanicsWithError("expected message", func() {
    panic(errors.Join(errors.New("surprising message")))
})

Fails with:

func (assert.PanicTestFunc)(0x84d4e0) should panic with error message:  "expected message"
                Panic value:    &errors.joinError{errs:[]error{(*errors.errorString)(0xc000119570)}}
                    Panic stack:    goroutine 19 [running]:

A real world example here is https://pkg.go.dev/text/template#ExecError.


Since the assertion expects this to be an error and it checks on the error message only, it would suffice to replace the listing of entire "Panic value" with panicErr.Error().

olivergondza commented 1 year ago

Proposing fix: https://github.com/stretchr/testify/pull/1400