Skip to content

Commit

Permalink
Exploit the second console.dir argument
Browse files Browse the repository at this point in the history
  • Loading branch information
xamgore committed Jun 20, 2020
1 parent 4471bbb commit 5c9f124
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/jest-console/src/BufferedConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import assert = require('assert');
import {Console} from 'console';
import {format} from 'util';
import {format, formatWithOptions, inspect} from 'util';
import chalk = require('chalk');
import {ErrorWithStack, formatTime} from 'jest-util';
import type {
Expand Down Expand Up @@ -98,8 +98,9 @@ export default class BufferedConsole extends Console {
this._log('debug', format(firstArg, ...rest));
}

dir(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('dir', format(firstArg, ...rest));
dir(firstArg: unknown, options: NodeJS.InspectOptions = {}): void {
const representation = inspect(firstArg, options);
this._log('dir', formatWithOptions(options, representation));
}

dirxml(firstArg: unknown, ...rest: Array<unknown>): void {
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import assert = require('assert');
import {format} from 'util';
import {format, formatWithOptions, inspect} from 'util';
import {Console} from 'console';
import chalk = require('chalk');
import {clearLine, formatTime} from 'jest-util';
Expand Down Expand Up @@ -75,8 +75,9 @@ export default class CustomConsole extends Console {
this._log('debug', format(firstArg, ...args));
}

dir(firstArg: unknown, ...args: Array<unknown>): void {
this._log('dir', format(firstArg, ...args));
dir(firstArg: unknown, options: NodeJS.InspectOptions = {}): void {
const representation = inspect(firstArg, options);
this._log('dir', formatWithOptions(options, representation));
}

dirxml(firstArg: unknown, ...args: Array<unknown>): void {
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-console/src/__tests__/CustomConsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,13 @@ describe('CustomConsole', () => {
expect(_stdout).toMatch('ms');
});
});

describe('dir', () => {
test('should print the deepest value', () => {
const deepObject = {1: {2: {3: {4: {5: {6: 'value'}}}}}};
_console.dir(deepObject, {depth: 6});

expect(_stdout).toMatch('value');
});
});
});
9 changes: 9 additions & 0 deletions packages/jest-console/src/__tests__/bufferedConsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,13 @@ describe('CustomConsole', () => {
expect(stdout()).toMatch('ms');
});
});

describe('dir', () => {
test('should print the deepest value', () => {
const deepObject = {1: {2: {3: {4: {5: {6: 'value'}}}}}};
_console.dir(deepObject, {depth: 6});

expect(stdout()).toMatch('value');
});
});
});

0 comments on commit 5c9f124

Please sign in to comment.