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

Stringify Errors properly with --json flag #15329

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

tonyd33
Copy link

@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!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2cbba58
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/66f8b7c1d32b14000850b256
😎 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.

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

Successfully merging this pull request may close these issues.

1 participant