Skip to content

Add PanicsWithErrorIs assertion#1353

Open
jannisbaudisch wants to merge 2 commits into
stretchr:masterfrom
jannisbaudisch:fix
Open

Add PanicsWithErrorIs assertion#1353
jannisbaudisch wants to merge 2 commits into
stretchr:masterfrom
jannisbaudisch:fix

Conversation

@jannisbaudisch
Copy link
Copy Markdown

@jannisbaudisch jannisbaudisch commented Feb 27, 2023

Summary

Adds new assertion method to test if a paniced error has a specific error in its chain

Changes

  • Add PanicsWithErrorIs with tests

Motivation

See my issue #1350.

I have some code that wraps a database error with fmt.Errorf() and panics it. Now I want to assert that this error is paniced.

The current method PanicsWithError only compares the error message, but does not use a error chain comparison.

Example:

func foo(source err) {
    panic(fmt.Errorf("Wrapping error %w", source))
}

func Test(t *testing.T) {
    expectedError := errors.New("some error")
    require.PanicsWithErrorIs(t, expectedError, func() { foo(expectedError) }) // This functionality is missing
}

@jannisbaudisch jannisbaudisch changed the title Fix Add PanicsWithErrorIs assertion Feb 27, 2023
@dolmen dolmen added enhancement pkg-require Change related to package testify/require pkg-assert Change related to package testify/assert type-error Issue related to comparing values implementing the error interface labels Jul 5, 2023
@dolmen dolmen added this to the v1.9.0 milestone Oct 16, 2023
@brackendawson brackendawson modified the milestones: v1.9.0, v1.10 Feb 28, 2024
@brackendawson brackendawson modified the milestones: v1.10, v1.11 Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require type-error Issue related to comparing values implementing the error interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants