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: console dir should respect options #10638

Merged
merged 11 commits into from Nov 10, 2020
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@

### Fixes

- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
- `[expect]` Fix `objectContaining` to work recursively into sub-objects ([#10508](https://github.com/facebook/jest/pull/10508))
- `[jest-message-util]` Update to work properly with Node 15 ([#10660](https://github.com/facebook/jest/pull/10660))
- `[jest-mock]` Allow to mock methods in getters (TypeScript 3.9 export) ([#10156](https://github.com/facebook/jest/pull/10156))
reckter marked this conversation as resolved.
Show resolved Hide resolved
- `[jest-config]` Fix bug introduced in watch mode by PR[#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692))
- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-console/src/BufferedConsole.ts
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 @@ -95,8 +95,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
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 @@ -73,8 +73,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
10 changes: 10 additions & 0 deletions packages/jest-console/src/__tests__/CustomConsole.test.ts
Expand Up @@ -187,6 +187,16 @@ describe('CustomConsole', () => {
});
});

describe('dir', () => {
reckter marked this conversation as resolved.
Show resolved Hide resolved
test('should print the deepest value', () => {
const deepObject = {1: {2: {3: {4: {5: {6: 'value'}}}}}};
_console.dir(deepObject, {depth: 6});

expect(_stdout).toMatch('value');
expect(_stdout).not.toMatch('depth');
});
});

describe('timeLog', () => {
test('should return the time between time() and timeEnd() on default timer', () => {
_console.time();
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-console/src/__tests__/bufferedConsole.test.ts
Expand Up @@ -147,6 +147,16 @@ describe('CustomConsole', () => {
});
});

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');
expect(stdout()).not.toMatch('depth');
});
});

describe('timeLog', () => {
test('should return the time between time() and timeEnd() on default timer', () => {
_console.time();
Expand Down