Skip to content

Commit 91cd04b

Browse files
authored
Merge branch 'main' into is-interactive-browser
2 parents fffac88 + 4f35f1f commit 91cd04b

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
### Fixes
1414

1515
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
16+
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
1617
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
1718
- `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552))
19+
- `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/facebook/jest/pull/14290))
1820

1921
### Performance
2022

packages/expect-utils/src/__tests__/utils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,16 @@ describe('arrayBufferEquality', () => {
571571
const b = Uint8Array.from([1, 2]).buffer;
572572
expect(arrayBufferEquality(a, b)).toBeTruthy();
573573
});
574+
575+
test('returns true when given matching DataView', () => {
576+
const a = new DataView(Uint8Array.from([1, 2, 3]).buffer);
577+
const b = new DataView(Uint8Array.from([1, 2, 3]).buffer);
578+
expect(arrayBufferEquality(a, b)).toBeTruthy();
579+
});
580+
581+
test('returns false when given matching DataView', () => {
582+
const a = new DataView(Uint8Array.from([1, 2, 3]).buffer);
583+
const b = new DataView(Uint8Array.from([3, 2, 1]).buffer);
584+
expect(arrayBufferEquality(a, b)).toBeFalsy();
585+
});
574586
});

packages/expect-utils/src/utils.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,17 @@ export const arrayBufferEquality = (
390390
a: unknown,
391391
b: unknown,
392392
): boolean | undefined => {
393-
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) {
394-
return undefined;
393+
let dataViewA = a;
394+
let dataViewB = b;
395+
396+
if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) {
397+
dataViewA = new DataView(a);
398+
dataViewB = new DataView(b);
395399
}
396400

397-
const dataViewA = new DataView(a);
398-
const dataViewB = new DataView(b);
401+
if (!(dataViewA instanceof DataView && dataViewB instanceof DataView)) {
402+
return undefined;
403+
}
399404

400405
// Buffers are not equal when they do not have the same byte length
401406
if (dataViewA.byteLength !== dataViewB.byteLength) {

packages/pretty-format/src/__tests__/prettyFormat.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ describe('prettyFormat()', () => {
7979

8080
it('prints an array buffer', () => {
8181
const val = new ArrayBuffer(3);
82-
expect(prettyFormat(val)).toBe('ArrayBuffer []');
82+
expect(prettyFormat(val)).toBe('ArrayBuffer [\n 0,\n 0,\n 0,\n]');
83+
});
84+
85+
it('prints an data view', () => {
86+
const val = new DataView(new ArrayBuffer(3));
87+
expect(prettyFormat(val)).toBe('DataView [\n 0,\n 0,\n 0,\n]');
8388
});
8489

8590
it('prints a nested array', () => {

packages/pretty-format/src/collections.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,33 +146,42 @@ export function printIteratorValues(
146146
* without surrounding punctuation (for example, brackets)
147147
**/
148148
export function printListItems(
149-
list: ArrayLike<unknown>,
149+
list: ArrayLike<unknown> | DataView | ArrayBuffer,
150150
config: Config,
151151
indentation: string,
152152
depth: number,
153153
refs: Refs,
154154
printer: Printer,
155155
): string {
156156
let result = '';
157+
list = list instanceof ArrayBuffer ? new DataView(list) : list;
158+
const isDataView = (l: unknown): l is DataView => l instanceof DataView;
159+
const length = isDataView(list) ? list.byteLength : list.length;
157160

158-
if (list.length > 0) {
161+
if (length > 0) {
159162
result += config.spacingOuter;
160163

161164
const indentationNext = indentation + config.indent;
162165

163-
for (let i = 0; i < list.length; i++) {
166+
for (let i = 0; i < length; i++) {
164167
result += indentationNext;
165168

166169
if (i === config.maxWidth) {
167170
result += '…';
168171
break;
169172
}
170173

171-
if (i in list) {
172-
result += printer(list[i], config, indentationNext, depth, refs);
174+
if (isDataView(list) || i in list) {
175+
result += printer(
176+
isDataView(list) ? list.getInt8(i) : list[i],
177+
config,
178+
indentationNext,
179+
depth,
180+
refs,
181+
);
173182
}
174183

175-
if (i < list.length - 1) {
184+
if (i < length - 1) {
176185
result += `,${config.spacingInner}`;
177186
} else if (!config.min) {
178187
result += ',';

0 commit comments

Comments
 (0)