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

Exploit the second console.dir argument #10182

Closed
wants to merge 1 commit into from
Closed
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
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this should be a snapshot so we can see the entire structure? same for the test below

Copy link
Contributor Author

@xamgore xamgore Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util.inspect implementation may differ from version to version as described in node.js documentation, but if that's ok for you, I'll introduce expect(deepObject).toMatchInlineSnapshot().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jest's output in this case shouldn't vary though, should it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you, please, rephrase the sentence? Not sure I properly understand you.

util.format may also differ from version to version, thus Jest's console.log output may vary from version to version. Same with console.dir.

Will Node.js core developers decide to change util.inspect for deep objects? Who knows, maybe they will add/remove extra spaces between colons. But value will always be in the output.

I'm not a core developer, so just say what to do, I'll do it.

});
});
});
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');
});
});
});