Skip to content

Commit

Permalink
feat(pretty-format): allow to opt out from sorting object keys with `…
Browse files Browse the repository at this point in the history
…compareKeys: null`
  • Loading branch information
Andarist committed Feb 20, 2022
1 parent c8a8456 commit 7da9111
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/jest-schemas/src/index.ts
Expand Up @@ -10,6 +10,7 @@ import {Static, Type} from '@sinclair/typebox';
const RawSnapshotFormat = Type.Partial(
Type.Object({
callToJSON: Type.Readonly(Type.Boolean()),
compareKeys: Type.Readonly(Type.Null()),
escapeRegex: Type.Readonly(Type.Boolean()),
escapeString: Type.Readonly(Type.Boolean()),
highlight: Type.Readonly(Type.Boolean()),
Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/README.md
Expand Up @@ -69,7 +69,7 @@ console.log(prettyFormat(onClick, options));
| key | type | default | description |
| :-------------------- | :-------- | :--------- | :------------------------------------------------------ |
| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
| `compareKeys` | `function`| `undefined`| compare function used when sorting object keys |
| `compareKeys` | `function|null`| `undefined`| compare function used when sorting object keys, `null` can be used to skip over sorting |
| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
| `escapeString` | `boolean` | `true` | escape special characters in strings |
| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
Expand Down Expand Up @@ -208,7 +208,7 @@ Write `serialize` to return a string, given the arguments:
| key | type | description |
| :------------------ | :-------- | :------------------------------------------------------ |
| `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects |
| `compareKeys` | `function`| compare function used when sorting object keys |
| `compareKeys` | `function|null`| compare function used when sorting object keys, `null` can be used to skip over sorting |
| `colors` | `Object` | escape codes for colors to highlight syntax |
| `escapeRegex` | `boolean` | escape special characters in regular expressions |
| `escapeString` | `boolean` | escape special characters in strings |
Expand Down
10 changes: 9 additions & 1 deletion packages/pretty-format/src/__tests__/prettyFormat.test.ts
Expand Up @@ -334,7 +334,7 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('Object {\n "a": 2,\n "b": 1,\n}');
});

it('prints an object with keys in their original order', () => {
it('prints an object with keys in their original order with the appropriate comparing function', () => {
// eslint-disable-next-line sort-keys
const val = {b: 1, a: 2};
const compareKeys = () => 0;
Expand All @@ -343,6 +343,14 @@ describe('prettyFormat()', () => {
);
});

it('prints an object with keys in their original order with compareKeys set to null', () => {
// eslint-disable-next-line sort-keys
const val = {b: 1, a: 2};
expect(prettyFormat(val, {compareKeys: null})).toEqual(
'Object {\n "b": 1,\n "a": 2,\n}',
);
});

it('prints an object with keys sorted in reverse order', () => {
const val = {a: 1, b: 2};
const compareKeys = (a: string, b: string) => (a > b ? -1 : 1);
Expand Down
4 changes: 3 additions & 1 deletion packages/pretty-format/src/collections.ts
Expand Up @@ -12,7 +12,9 @@ const getKeysOfEnumerableProperties = (
object: Record<string, unknown>,
compareKeys: CompareKeys,
) => {
const keys: Array<string | symbol> = Object.keys(object).sort(compareKeys);
const rawKeys = Object.keys(object);
const keys: Array<string | symbol> =
compareKeys !== null ? rawKeys.sort(compareKeys) : rawKeys;

if (Object.getOwnPropertySymbols) {
Object.getOwnPropertySymbols(object).forEach(symbol => {
Expand Down
3 changes: 2 additions & 1 deletion packages/pretty-format/src/index.ts
Expand Up @@ -489,7 +489,8 @@ const getConfig = (options?: OptionsReceived): Config => ({
? getColorsHighlight(options)
: getColorsEmpty(),
compareKeys:
options && typeof options.compareKeys === 'function'
options &&
(typeof options.compareKeys === 'function' || options.compareKeys === null)
? options.compareKeys
: DEFAULT_OPTIONS.compareKeys,
escapeRegex: getEscapeRegex(options),
Expand Down
5 changes: 3 additions & 2 deletions packages/pretty-format/src/types.ts
Expand Up @@ -20,7 +20,7 @@ type Print = (arg0: unknown) => string;

export type Theme = Options['theme'];

export type CompareKeys = ((a: string, b: string) => number) | undefined;
export type CompareKeys = ((a: string, b: string) => number) | null | undefined;

type RequiredOptions = Required<PrettyFormatOptions>;

Expand All @@ -30,7 +30,8 @@ export interface Options
theme: Required<RequiredOptions['theme']>;
}

export interface PrettyFormatOptions extends SnapshotFormat {
export interface PrettyFormatOptions
extends Omit<SnapshotFormat, 'compareKeys'> {
compareKeys?: CompareKeys;
plugins?: Plugins;
}
Expand Down

0 comments on commit 7da9111

Please sign in to comment.