Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the stacktrace of the err not the stack trace of the Expect #739

Open
dschveninger opened this issue Mar 6, 2024 · 2 comments

Comments

@dschveninger
Copy link

Expect(err).ShouldNot(HaveOccurred())
or
Expect(err).ToNot(HaveOccurred())

Only give you the Expect stackTrace or the stackTrace of the DSL. We are looking for the stackTrace of the error

Example output of Expect(err).ShouldNot(HaveOccurred())

Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      One function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21
  > Enter [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21 @ 03/06/24 14:37:11.239
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019e810>:
      Error cam from FunctionOne
      {
          msg: "Error cam from FunctionOne",
          stack: [0x73e45c, 0x73e5bc, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:23 @ 03/06/24 14:37:11.239

  Full Stack Trace
  < Exit [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21 @ 03/06/24 14:37:11.24 (1ms)
• [FAILED] [0.001 seconds]

When we use github.com/pkg/error
and
Expect(err).NotTo(HaveOccurred(), "Error: %+v", err)
we get a better stackTrace

Example output of Expect(err).NotTo(HaveOccurred(), "Error: %+v", err)

Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      [It] three function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:43
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    One function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51
  > Enter [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51 @ 03/06/24 14:37:11.243
  [FAILED] Error: Error cam from FunctionOne
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionOne
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:69
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.glob..func1.2.1
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:52
  github.com/onsi/ginkgo/v2/internal.extractBodyFunction.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/node.go:463
  github.com/onsi/ginkgo/v2/internal.(*Suite).runNode.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/suite.go:889
  runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1650
  Unexpected error:
      <*errors.fundamental | 0xc00019f080>:
      Error cam from FunctionOne
      {
          msg: "Error cam from FunctionOne",
          stack: [0x73e45c, 0x73eb45, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:53 @ 03/06/24 14:37:11.243

  Full Stack Trace
  < Exit [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51 @ 03/06/24 14:37:11.243 (0s)
• [FAILED] [0.000 seconds]

What is the proper way to get the Stack Trace of the error?

Code example

package ginkgo_error_test

import (
	"testing"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	"github.com/pkg/errors"
)

//nolint:paralleltest // Not needed in Ginkgo suite setup.
func TestUserRp(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Examples")
}

var _ = Describe("Stack Trace examples", Label("issues"), func() {
	Context("Standard Approach", func() {
		Context("ShouldNot", func() {
			It("One function deep", func() {
				err := FunctionOne(true, false, false)
				Expect(err).ShouldNot(HaveOccurred())
			})
			It("two function deep", func() {
				err := FunctionOne(false, true, false)
				Expect(err).ShouldNot(HaveOccurred())
			})
			It("three function deep", func() {
				err := FunctionOne(false, false, true)
				Expect(errors.Cause(err)).ShouldNot(HaveOccurred())
			})
		})
		Context("NotTo", func() {
			It("One function deep", func() {
				err := FunctionOne(true, false, false)
				Expect(err).NotTo(HaveOccurred())
			})
			It("two function deep", func() {
				err := FunctionOne(false, true, false)
				Expect(err).NotTo(HaveOccurred())
			})
			It("three function deep", func() {
				err := FunctionOne(false, false, true)
				Expect(err).NotTo(HaveOccurred())
			})
		})
	})
	// this direction will give you the stack trace of the Expect not the called function that had the error
	Context("using github.com/pkg/errors", func() {
		It("One function deep", func() {
			err := FunctionOne(true, false, false)
			Expect(err).NotTo(HaveOccurred(), "Error: %+v", err)
		})
		It("two function deep", func() {
			err := FunctionOne(false, true, false)
			Expect(err).NotTo(HaveOccurred(), "Error: %+v", err)
		})
		It("three function deep", func() {
			err := FunctionOne(false, false, true)
			Expect(err).NotTo(HaveOccurred(), "Error: %+v", err)
		})

	})
})

func FunctionOne(oneReturnError, twoReturnError, threeReturnError bool) error {
	if oneReturnError {
		return errors.New("Error cam from FunctionOne")
	}
	return FunctionTwo(twoReturnError, threeReturnError)
}

func FunctionTwo(twoReturnError, threeReturnError bool) error {
	if twoReturnError {
		return errors.New("Error cam from FunctionTwo")
	}
	return FunctionThree(threeReturnError)
}

func FunctionThree(threeReturnError bool) error {
	if threeReturnError {
		return errors.New("Error cam from FunctionThree")
	}
	return nil
}

Output from code above run

user@LAPTOP-GLS580EI:~/nc-api-testing$ ginkgo run  --trace --vv ./tests/prototype/ginkgo/errors/...
Running Suite: Examples - /home/user/nc-api-testing/tests/prototype/ginkgo/errors
=========================================================================================
Random Seed: 1709757430

Will run 9 of 9 specs
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      One function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21
  > Enter [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21 @ 03/06/24 14:37:11.239
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019e810>:
      Error cam from FunctionOne
      {
          msg: "Error cam from FunctionOne",
          stack: [0x73e45c, 0x73e5bc, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:23 @ 03/06/24 14:37:11.239

  Full Stack Trace
  < Exit [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21 @ 03/06/24 14:37:11.24 (1ms)
• [FAILED] [0.001 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      [It] One function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:21
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      two function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:25
  > Enter [It] two function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:25 @ 03/06/24 14:37:11.24
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019e978>:
      Error cam from FunctionTwo
      {
          msg: "Error cam from FunctionTwo",
          stack: [0x73e572, 0x73e474, 0x73e63c, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:27 @ 03/06/24 14:37:11.24

  Full Stack Trace
  < Exit [It] two function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:25 @ 03/06/24 14:37:11.24 (1ms)
• [FAILED] [0.001 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      [It] two function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:25
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      three function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:29
  > Enter [It] three function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:29 @ 03/06/24 14:37:11.241
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019eae0>:
      Error cam from FunctionThree
      {
          msg: "Error cam from FunctionThree",
          stack: [0x73e4c3, 0x73e4c1, 0x73e474, 0x73e6bc, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:31 @ 03/06/24 14:37:11.241

  Full Stack Trace
  < Exit [It] three function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:29 @ 03/06/24 14:37:11.241 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    ShouldNot
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:20
      [It] three function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:29
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      One function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:35
  > Enter [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:35 @ 03/06/24 14:37:11.241
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019ec48>:
      Error cam from FunctionOne
      {
          msg: "Error cam from FunctionOne",
          stack: [0x73e45c, 0x73e81c, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:37 @ 03/06/24 14:37:11.242

  Full Stack Trace
  < Exit [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:35 @ 03/06/24 14:37:11.242 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      [It] One function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:35
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      two function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:39
  > Enter [It] two function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:39 @ 03/06/24 14:37:11.242
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019edb0>:
      Error cam from FunctionTwo
      {
          msg: "Error cam from FunctionTwo",
          stack: [0x73e572, 0x73e474, 0x73e89c, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:41 @ 03/06/24 14:37:11.242

  Full Stack Trace
  < Exit [It] two function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:39 @ 03/06/24 14:37:11.242 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      [It] two function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:39
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      three function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:43
  > Enter [It] three function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:43 @ 03/06/24 14:37:11.242
  [FAILED] Unexpected error:
      <*errors.fundamental | 0xc00019ef18>:
      Error cam from FunctionThree
      {
          msg: "Error cam from FunctionThree",
          stack: [0x73e4c3, 0x73e4c1, 0x73e474, 0x73e91c, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:45 @ 03/06/24 14:37:11.242

  Full Stack Trace
  < Exit [It] three function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:43 @ 03/06/24 14:37:11.243 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  Standard Approach
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:19
    NotTo
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:34
      [It] three function deep
      /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:43
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    One function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51
  > Enter [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51 @ 03/06/24 14:37:11.243
  [FAILED] Error: Error cam from FunctionOne
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionOne
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:69
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.glob..func1.2.1
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:52
  github.com/onsi/ginkgo/v2/internal.extractBodyFunction.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/node.go:463
  github.com/onsi/ginkgo/v2/internal.(*Suite).runNode.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/suite.go:889
  runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1650
  Unexpected error:
      <*errors.fundamental | 0xc00019f080>:
      Error cam from FunctionOne
      {
          msg: "Error cam from FunctionOne",
          stack: [0x73e45c, 0x73eb45, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:53 @ 03/06/24 14:37:11.243

  Full Stack Trace
  < Exit [It] One function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51 @ 03/06/24 14:37:11.243 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    [It] One function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:51
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    two function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:55
  > Enter [It] two function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:55 @ 03/06/24 14:37:11.243
  [FAILED] Error: Error cam from FunctionTwo
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionTwo
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:76
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionOne
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:71
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.glob..func1.2.2
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:56
  github.com/onsi/ginkgo/v2/internal.extractBodyFunction.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/node.go:463
  github.com/onsi/ginkgo/v2/internal.(*Suite).runNode.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/suite.go:889
  runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1650
  Unexpected error:
      <*errors.fundamental | 0xc00019f1e8>:
      Error cam from FunctionTwo
      {
          msg: "Error cam from FunctionTwo",
          stack: [0x73e572, 0x73e474, 0x73ec45, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:57 @ 03/06/24 14:37:11.243

  Full Stack Trace
  < Exit [It] two function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:55 @ 03/06/24 14:37:11.244 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    [It] two function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:55
------------------------------
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    three function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:59
  > Enter [It] three function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:59 @ 03/06/24 14:37:11.244
  [FAILED] Error: Error cam from FunctionThree
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionThree
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:83
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionTwo
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:78
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.FunctionOne
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:71
  dev.azure.com/msazuredev/AzureForOperatorsIndustry/_git/nc-api-testing/tests/prototype/ginkgo/errors_test.glob..func1.2.3
        /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:60
  github.com/onsi/ginkgo/v2/internal.extractBodyFunction.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/node.go:463
  github.com/onsi/ginkgo/v2/internal.(*Suite).runNode.func3
        /home/user/go/pkg/mod/github.com/onsi/ginkgo/v2@v2.15.0/internal/suite.go:889
  runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1650
  Unexpected error:
      <*errors.fundamental | 0xc00019f350>:
      Error cam from FunctionThree
      {
          msg: "Error cam from FunctionThree",
          stack: [0x73e4c3, 0x73e4c1, 0x73e474, 0x73ed45, 0x717213, 0x7287cd, 0x46ef61],
      }
  occurred
  In [It] at: /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:61 @ 03/06/24 14:37:11.244

  Full Stack Trace
  < Exit [It] three function deep - /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:59 @ 03/06/24 14:37:11.244 (0s)
• [FAILED] [0.000 seconds]
Stack Trace examples [issues]
/home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:18
  using github.com/pkg/errors
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:50
    [It] three function deep
    /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:59
------------------------------

Summarizing 9 Failures:
  [FAIL] Stack Trace examples Standard Approach ShouldNot [It] One function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:23
  [FAIL] Stack Trace examples Standard Approach ShouldNot [It] two function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:27
  [FAIL] Stack Trace examples Standard Approach ShouldNot [It] three function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:31
  [FAIL] Stack Trace examples Standard Approach NotTo [It] One function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:37
  [FAIL] Stack Trace examples Standard Approach NotTo [It] two function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:41
  [FAIL] Stack Trace examples Standard Approach NotTo [It] three function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:45
  [FAIL] Stack Trace examples using github.com/pkg/errors [It] One function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:53
  [FAIL] Stack Trace examples using github.com/pkg/errors [It] two function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:57
  [FAIL] Stack Trace examples using github.com/pkg/errors [It] three function deep [issues]
  /home/user/nc-api-testing/tests/prototype/ginkgo/errors/ginkgo_error_test.go:61

Ran 9 of 9 Specs in 0.006 seconds
FAIL! -- 0 Passed | 9 Failed | 0 Pending | 0 Skipped
--- FAIL: TestUserRp (0.01s)
FAIL

Ginkgo ran 1 suite in 533.1534ms

Test Suite Failed
@onsi
Copy link
Owner

onsi commented Mar 10, 2024

hey @dschveninger - the Ginkgo stack trace always simply points to the location of the failure. In this case an Expect(..)... is failing and Ginkgo is pointing to that failure.

It is up to the Gomega matcher to provide whatever information is necessary to adequately describe the failure to the user. In this case, since Gomega simply tells you that an error occurred. Since Go doesn't attach stack traces to errors by default there isn't anything Gomega can do out of the box to provide you with that information. (An error is just a value in Go - it doens't intrinsically contain any context about where it was generated outside of what you explicitly inject into it)

If, however, you use an error library that does provide stack traces then you have three options:

  1. you can build a custom matcher that parallels HaveOccurred but emits the stack trace
  2. you can wrap error objects with a struct that implements GomegaStringer to generate custom output that includes the stack trace.
  3. (perhaps easiest) you can register a custom formatter that detects the special stack trace error objects and renders the stack trace for you.

@dschveninger
Copy link
Author

Let's us try some of your suggestion. Thank you for the ideas. Once we find the best direction, we will update this issue and close it if you do not mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants