testify icon indicating copy to clipboard operation
testify copied to clipboard

Improve ErrorIs message when error is nil but an error was expected

Open tsioftas opened this issue 1 year ago • 0 comments

Summary

This improves the UX of users of ErrorIs() in case the value nil is passed for err while an error was expected.

Changes

  • added check if err == nil && target != nil -> return a special error-string
  • updated relevant test case

Motivation

The error message when an error is expected but err is nil (a common occurence in the world of testing) could be more helpful, e.g.:

(before)
some_test.go:20:
                Error Trace:    /[...]/some_test.go:20
                Error:          Target error should be in err chain:
                                expected: "sample error message"
                                in chain:

vs

(after)
some_test.go:20:
                Error Trace:    /[...]/some_test.go:20
                Error:          Expected error "sample error message" but got nil.

tsioftas avatar Nov 18 '24 12:11 tsioftas