Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pretty-format): print ArrayBuffer and DataView incorrectly #14290

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion packages/pretty-format/src/__tests__/prettyFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
21 changes: 15 additions & 6 deletions packages/pretty-format/src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,33 +146,42 @@ export function printIteratorValues(
* without surrounding punctuation (for example, brackets)
**/
export function printListItems(
list: ArrayLike<unknown>,
list: ArrayLike<unknown> | DataView | ArrayBuffer,
config: Config,
indentation: string,
depth: number,
refs: Refs,
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) {
result += '…';
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 += ',';
Expand Down