Stringify Errors properly with --json flag #15329
Open
+89
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
When running
jest
with the--json
flag, theError
s fromcollectHandles
injest-core/src/runJest.ts
is all passed into aJSON.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
inrunJest.ts:processResults
to grab the error message, name, and stack from theError
, but adding a test for that was kind of awkward.So I decided on adding a very basic
serializeToJSON
function that should replaceJSON.stringify
to handle stringifyingError
s as well as leaving room to stringify other objects in the future if needed.Test plan
serializeToJSON
JSON.stringify
for regular objectsJSON.stringify
by stringifyingError
s properlyFor a more e2e test, I added a temporary test to leak a handle, built the project, and ran the test: