From 4423c35b81d572226e4d599b4e6410d0dcae6fb2 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 30 Jun 2023 02:06:04 +0800 Subject: [PATCH 1/2] fix(pretty-format): print ArrayBuffer and DataView incorrectly --- .../src/__tests__/prettyFormat.test.ts | 7 ++++++- packages/pretty-format/src/collections.ts | 21 +++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/pretty-format/src/__tests__/prettyFormat.test.ts b/packages/pretty-format/src/__tests__/prettyFormat.test.ts index 6a3e3f972ec5..9f8ac28f3a59 100644 --- a/packages/pretty-format/src/__tests__/prettyFormat.test.ts +++ b/packages/pretty-format/src/__tests__/prettyFormat.test.ts @@ -79,7 +79,12 @@ describe('prettyFormat()', () => { it('prints an array buffer', () => { const val = new ArrayBuffer(3); - expect(prettyFormat(val)).toBe('ArrayBuffer []'); + expect(prettyFormat(val)).toBe('ArrayBuffer [\n 0,\n 0,\n 0,\n]'); + }); + + it('prints an data view', () => { + const val = new DataView(new ArrayBuffer(3)); + expect(prettyFormat(val)).toBe('DataView [\n 0,\n 0,\n 0,\n]'); }); it('prints a nested array', () => { diff --git a/packages/pretty-format/src/collections.ts b/packages/pretty-format/src/collections.ts index c287b984410a..1a4404ac30a8 100644 --- a/packages/pretty-format/src/collections.ts +++ b/packages/pretty-format/src/collections.ts @@ -146,7 +146,7 @@ export function printIteratorValues( * without surrounding punctuation (for example, brackets) **/ export function printListItems( - list: ArrayLike, + list: ArrayLike | DataView | ArrayBuffer, config: Config, indentation: string, depth: number, @@ -154,13 +154,16 @@ export function printListItems( printer: Printer, ): string { let result = ''; + list = list instanceof ArrayBuffer ? new DataView(list) : list; + const isDataView = (l: unknown): l is DataView => l instanceof DataView; + const length = isDataView(list) ? list.byteLength : list.length; - if (list.length) { + if (length) { result += config.spacingOuter; const indentationNext = indentation + config.indent; - for (let i = 0; i < list.length; i++) { + for (let i = 0; i < length; i++) { result += indentationNext; if (i === config.maxWidth) { @@ -168,11 +171,17 @@ export function printListItems( break; } - if (i in list) { - result += printer(list[i], config, indentationNext, depth, refs); + if (isDataView(list) || i in list) { + result += printer( + isDataView(list) ? list.getInt8(i) : list[i], + config, + indentationNext, + depth, + refs, + ); } - if (i < list.length - 1) { + if (i < length - 1) { result += `,${config.spacingInner}`; } else if (!config.min) { result += ','; From a26696190b0cc194fe149b8c3f912edcdb4f1c54 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 20 Sep 2023 09:05:27 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 525ddd793b52..09f79fdb13e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Fixes - `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526)) +- `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/facebook/jest/pull/14290)) ### Performance