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 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 d87fd41132e8..bd25e657f084 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 > 0) { + if (length > 0) { 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 += ',';