Skip to content

Commit

Permalink
feat: Better print for sparse arrays (#11326)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Apr 22, 2021
1 parent 57c3c07 commit 7028265
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -37,6 +37,7 @@
- `[jest-transform]` Support transpiled transformers ([#11193](https://github.com/facebook/jest/pull/11193))
- `[jest-worker]` Add support for custom task queues and adds a `PriorityQueue` implementation. ([#10921](https://github.com/facebook/jest/pull/10921))
- `[jest-worker]` Add in-order scheduling policy to jest worker ([10902](https://github.com/facebook/jest/pull/10902))
- `[pretty-format]` Better print for sparse arrays ([11326](https://github.com/facebook/jest/pull/11326))

### Fixes

Expand Down
26 changes: 26 additions & 0 deletions packages/pretty-format/src/__tests__/prettyFormat.test.ts
Expand Up @@ -41,6 +41,32 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('Array [\n 1,\n 2,\n 3,\n]');
});

it('prints a sparse array with only holes', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [, , ,];
expect(prettyFormat(val)).toEqual('Array [\n ,\n ,\n ,\n]');
});

it('prints a sparse array with items', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [1, , , 4];
expect(prettyFormat(val)).toEqual('Array [\n 1,\n ,\n ,\n 4,\n]');
});

it('prints a sparse array with value surrounded by holes', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [, 5, ,];
expect(prettyFormat(val)).toEqual('Array [\n ,\n 5,\n ,\n]');
});

it('prints a sparse array also containing undefined values', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [1, , undefined, undefined, , 4];
expect(prettyFormat(val)).toEqual(
'Array [\n 1,\n ,\n undefined,\n undefined,\n ,\n 4,\n]',
);
});

it('prints a empty typed array', () => {
const val = new Uint32Array(0);
expect(prettyFormat(val)).toEqual('Uint32Array []');
Expand Down
8 changes: 5 additions & 3 deletions packages/pretty-format/src/collections.ts
Expand Up @@ -142,9 +142,11 @@ export function printListItems(
const indentationNext = indentation + config.indent;

for (let i = 0; i < list.length; i++) {
result +=
indentationNext +
printer(list[i], config, indentationNext, depth, refs);
result += indentationNext;

if (i in list) {
result += printer(list[i], config, indentationNext, depth, refs);
}

if (i < list.length - 1) {
result += ',' + config.spacingInner;
Expand Down

0 comments on commit 7028265

Please sign in to comment.