diff --git a/CHANGELOG.md b/CHANGELOG.md index d8bfbfa97d82..8cbfb7b613fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes +- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638)) - `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708)) - `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781)) - `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644)) diff --git a/packages/jest-console/src/BufferedConsole.ts b/packages/jest-console/src/BufferedConsole.ts index 55c4e20ffc95..8d67c968dea7 100644 --- a/packages/jest-console/src/BufferedConsole.ts +++ b/packages/jest-console/src/BufferedConsole.ts @@ -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): 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): void { diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts index 5c3ed54da576..cbf8cf768ee3 100644 --- a/packages/jest-console/src/CustomConsole.ts +++ b/packages/jest-console/src/CustomConsole.ts @@ -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 {clearLine, formatTime} from 'jest-util'; import type {LogCounters, LogMessage, LogTimers, LogType} from './types'; @@ -73,8 +73,9 @@ export default class CustomConsole extends Console { this._log('debug', format(firstArg, ...args)); } - dir(firstArg: unknown, ...args: Array): 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): void { diff --git a/packages/jest-console/src/__tests__/CustomConsole.test.ts b/packages/jest-console/src/__tests__/CustomConsole.test.ts index 07bb5cf4f888..b4c1b6d4e0d0 100644 --- a/packages/jest-console/src/__tests__/CustomConsole.test.ts +++ b/packages/jest-console/src/__tests__/CustomConsole.test.ts @@ -187,6 +187,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(); diff --git a/packages/jest-console/src/__tests__/bufferedConsole.test.ts b/packages/jest-console/src/__tests__/bufferedConsole.test.ts index 8b0ee9f98439..59e37ebfd116 100644 --- a/packages/jest-console/src/__tests__/bufferedConsole.test.ts +++ b/packages/jest-console/src/__tests__/bufferedConsole.test.ts @@ -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();