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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

### Fixes

- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
- `[jest-runtime]` `require.main` is no longer `undefined` when using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626))
- `[@jest/types]` Add missing values for `timers` ([#10632](https://github.com/facebook/jest/pull/10632))

7 changes: 4 additions & 3 deletions packages/jest-console/src/BufferedConsole.ts
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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 {
7 changes: 4 additions & 3 deletions packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
@@ -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';
@@ -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 {
10 changes: 10 additions & 0 deletions packages/jest-console/src/__tests__/CustomConsole.test.ts
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 10 additions & 0 deletions packages/jest-console/src/__tests__/bufferedConsole.test.ts
Original file line number Diff line number Diff line change
@@ -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();