Skip to content

Commit

Permalink
add stringifying errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyd33 committed Sep 29, 2024
1 parent 7b5e820 commit 5f85233
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
49 changes: 49 additions & 0 deletions packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts
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,

Check failure on line 13 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in ascending order. 'ok' should be before 'species'
i: ['pull up'],

Check failure on line 14 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in ascending order. 'i' should be before 'ok'
hopOut: {

Check failure on line 15 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in ascending order. 'hopOut' should be before 'i'
atThe: 'after party',
when: new Date('2000-07-14'),
},
chillness: 100,

Check failure on line 19 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in ascending order. 'chillness' should be before 'hopOut'
weight: 9.5,
flaws: null,

Check failure on line 21 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in ascending order. 'flaws' should be before 'weight'
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"`);

Check failure on line 39 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
expect(withError).toContain(`"name":"Error"`);

Check failure on line 40 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
expect(withError).toContain(`"stack":"Error:`);

Check failure on line 41 in packages/jest-core/src/lib/__tests__/serializeToJSON.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

expect(JSON.parse(withError)).toMatchObject({
error: {
message: 'too cool',
name: 'Error',
},
});
});
35 changes: 35 additions & 0 deletions packages/jest-core/src/lib/serializeToJSON.ts
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,

Check failure on line 16 in packages/jest-core/src/lib/serializeToJSON.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument 'value' should be typed with a non-any type
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,
);
}
6 changes: 4 additions & 2 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import collectNodeHandles, {
import getNoTestsFoundMessage from './getNoTestsFoundMessage';
import runGlobalHook from './runGlobalHook';
import type {Filter, TestRunData} from './types';
import serializeToJSON from './lib/serializeToJSON';

Check failure on line 38 in packages/jest-core/src/runJest.ts

View workflow job for this annotation

GitHub Actions / Lint

`./lib/serializeToJSON` import should occur before import of `./runGlobalHook`

const getTestPaths = async (
globalConfig: Config.GlobalConfig,
Expand Down Expand Up @@ -111,20 +112,21 @@ const processResults = async (
runResults = await processor(runResults);
}
if (isJSON) {
const jsonString = serializeToJSON(formatTestResults(runResults));
if (outputFile) {
const cwd = tryRealpath(process.cwd());
const filePath = path.resolve(cwd, outputFile);

fs.writeFileSync(
filePath,
`${JSON.stringify(formatTestResults(runResults))}\n`,
`${jsonString}\n`,
);
outputStream.write(
`Test results written to: ${path.relative(cwd, filePath)}\n`,
);
} else {
process.stdout.write(
`${JSON.stringify(formatTestResults(runResults))}\n`,
`${jsonString}\n`,
);
}
}
Expand Down

0 comments on commit 5f85233

Please sign in to comment.