jest icon indicating copy to clipboard operation
jest copied to clipboard

Stringify Errors properly with --json flag

Open tonyd33 opened this issue 1 year ago • 2 comments

Summary

When running jest with the --json flag, the Errors from collectHandles in jest-core/src/runJest.ts is all passed into a JSON.stringify, which turns them into an empty object.

Users might expect or want the error message, name, and stack from the --json output possibly to collect data in the more structured JSON format — as opposed to plain stderr output — to help diagnose the source of the leaky handles.

Changes

Originally, I just iterated over the runResults.openHandles in runJest.ts:processResults to grab the error message, name, and stack from the Error, but adding a test for that was kind of awkward.

So I decided on adding a very basic serializeToJSON function that should replace JSON.stringify to handle stringifying Errors as well as leaving room to stringify other objects in the future if needed.

Test plan

  • Added tests for serializeToJSON
    • Check that it's the same as JSON.stringify for regular objects
    • Check that it extends JSON.stringify by stringifying Errors properly

For a more e2e test, I added a temporary test to leak a handle, built the project, and ran the test: image

tonyd33 avatar Sep 29 '24 02:09 tonyd33

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: tonyd33 / name: Tony (5f85233fb683962643adaa1d6423648fc24e7bac, 2cbba58bcadd9eef891ccf7cbc7b5d3f26b504fc)
  • :white_check_mark: login: SimenB / name: Simen Bekkhus (78771159651b332bb18bcde5d3aba70c69e3e71b, 5caff62d28b9145e186525d7b93017c5eff50718, 24bd16d215c1a8cab8e159233d9f8e4570ddb597, 59cf0558f37621956a80062b7ab39eb4550cfc09)

Deploy Preview for jestjs ready!

Name Link
Latest commit 78771159651b332bb18bcde5d3aba70c69e3e71b
Latest deploy log https://app.netlify.com/sites/jestjs/deploys/6787ab225a0ca600080ec558
Deploy Preview https://deploy-preview-15329--jestjs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Sep 29 '24 02:09 netlify[bot]

This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 28 '24 02:12 github-actions[bot]

https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.7

SimenB avatar Jan 30 '25 08:01 SimenB

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Mar 02 '25 00:03 github-actions[bot]