Stringify Errors properly with --json flag
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.stringifyfor regular objects - Check that it extends
JSON.stringifyby stringifyingErrors properly
- Check that it's the same as
For a more e2e test, I added a temporary test to leak a handle, built the project, and ran the test:
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...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
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.
https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.7
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.