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

feat: stringify Errors properly with --json flag #15329

Merged
merged 6 commits into from
Jan 15, 2025
Merged

Conversation

tonyd33
Copy link
Contributor

@tonyd33 tonyd33 commented Sep 29, 2024

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

Copy link

linux-foundation-easycla bot commented Sep 29, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link

netlify bot commented Sep 29, 2024

Deploy Preview for jestjs ready!

Name Link
🔨 Latest commit 7877115
🔍 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.

Copy link

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 github-actions bot added the Stale label Dec 28, 2024
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@SimenB SimenB changed the title Stringify Errors properly with --json flag feat: stringify Errors properly with --json flag Jan 15, 2025
@SimenB SimenB enabled auto-merge (squash) January 15, 2025 12:33
@SimenB SimenB merged commit afc0cfb into jestjs:main Jan 15, 2025
83 of 84 checks passed
mohammednumaan pushed a commit to mohammednumaan/jest that referenced this pull request Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants