From 7da911120b9c497aee80402ba2fb8c2725c07b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 20 Feb 2022 19:56:03 +0100 Subject: [PATCH] feat(pretty-format): allow to opt out from sorting object keys with `compareKeys: null` --- packages/jest-schemas/src/index.ts | 1 + packages/pretty-format/README.md | 4 ++-- .../pretty-format/src/__tests__/prettyFormat.test.ts | 10 +++++++++- packages/pretty-format/src/collections.ts | 4 +++- packages/pretty-format/src/index.ts | 3 ++- packages/pretty-format/src/types.ts | 5 +++-- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/jest-schemas/src/index.ts b/packages/jest-schemas/src/index.ts index b3f0a32eebe8..d91c4c87a802 100644 --- a/packages/jest-schemas/src/index.ts +++ b/packages/jest-schemas/src/index.ts @@ -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()), diff --git a/packages/pretty-format/README.md b/packages/pretty-format/README.md index c5cb0041dbfa..b3405d4cebdd 100755 --- a/packages/pretty-format/README.md +++ b/packages/pretty-format/README.md @@ -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) | @@ -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 | diff --git a/packages/pretty-format/src/__tests__/prettyFormat.test.ts b/packages/pretty-format/src/__tests__/prettyFormat.test.ts index ab9d0b9da214..347a9bf2bf6d 100644 --- a/packages/pretty-format/src/__tests__/prettyFormat.test.ts +++ b/packages/pretty-format/src/__tests__/prettyFormat.test.ts @@ -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; @@ -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); diff --git a/packages/pretty-format/src/collections.ts b/packages/pretty-format/src/collections.ts index 97fae10163ba..ef87b9e9e467 100644 --- a/packages/pretty-format/src/collections.ts +++ b/packages/pretty-format/src/collections.ts @@ -12,7 +12,9 @@ const getKeysOfEnumerableProperties = ( object: Record, compareKeys: CompareKeys, ) => { - const keys: Array = Object.keys(object).sort(compareKeys); + const rawKeys = Object.keys(object); + const keys: Array = + compareKeys !== null ? rawKeys.sort(compareKeys) : rawKeys; if (Object.getOwnPropertySymbols) { Object.getOwnPropertySymbols(object).forEach(symbol => { diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index 9543e6b354a8..44674527670d 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -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), diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index c26b77aee0f7..06387e727bfc 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -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; @@ -30,7 +30,8 @@ export interface Options theme: Required; } -export interface PrettyFormatOptions extends SnapshotFormat { +export interface PrettyFormatOptions + extends Omit { compareKeys?: CompareKeys; plugins?: Plugins; }