-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import serializeToJSON from '../serializeToJSON'; | ||
|
||
// populate an object with all basic JavaScript datatypes | ||
const object = { | ||
species: 'capybara', | ||
ok: true, | ||
i: ['pull up'], | ||
hopOut: { | ||
atThe: 'after party', | ||
when: new Date('2000-07-14'), | ||
}, | ||
chillness: 100, | ||
weight: 9.5, | ||
flaws: null, | ||
location: undefined, | ||
}; | ||
|
||
it('serializes regular objects like JSON.stringify', () => { | ||
expect(serializeToJSON(object)).toEqual(JSON.stringify(object)); | ||
}); | ||
|
||
it('serializes errors', () => { | ||
const objectWithError = { | ||
...object, | ||
error: new Error('too cool'), | ||
}; | ||
const withError = serializeToJSON(objectWithError); | ||
const withoutError = JSON.stringify(objectWithError); | ||
|
||
expect(withoutError).not.toEqual(withError); | ||
|
||
expect(withError).toContain(`"message":"too cool"`); | ||
expect(withError).toContain(`"name":"Error"`); | ||
expect(withError).toContain(`"stack":"Error:`); | ||
|
||
expect(JSON.parse(withError)).toMatchObject({ | ||
error: { | ||
message: 'too cool', | ||
name: 'Error', | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* When we're asked to give a JSON output with the --json flag or otherwise, | ||
* some data we need to return don't serialize well with a basic | ||
* `JSON.stringify`, particularly Errors returned in `.openHandles`. | ||
* | ||
* This function handles the extended serialization wanted above. | ||
*/ | ||
export default function serializeToJSON( | ||
value: any, | ||
space?: string | number, | ||
): string { | ||
return JSON.stringify( | ||
value, | ||
(_, value) => { | ||
// There might be more in Error, but pulling out just the message, name, | ||
// and stack should be good enough | ||
if (value instanceof Error) { | ||
return { | ||
message: value.message, | ||
name: value.name, | ||
stack: value.stack, | ||
}; | ||
} | ||
return value; | ||
}, | ||
space, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters