diff --git a/CHANGELOG.md b/CHANGELOG.md index fcd2ed19695a..7507d751ee0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ([#5560](https://github.com/facebook/jest/pull/5560)) * `[jest-config]` Make it possible to merge `transform` option with preset ([#5505](https://github.com/facebook/jest/pull/5505)) +* `[jest-util]` Fix `console.assert` behavior in custom & buffered consoles + ([#5576](https://github.com/facebook/jest/pull/5576)) ### Features diff --git a/packages/jest-util/src/Console.js b/packages/jest-util/src/Console.js index 3f7f109a7284..2ec73a81e1ed 100644 --- a/packages/jest-util/src/Console.js +++ b/packages/jest-util/src/Console.js @@ -10,6 +10,7 @@ import type {LogType, LogMessage, LogCounters, LogTimers} from 'types/Console'; +import assert from 'assert'; import {format} from 'util'; import {Console} from 'console'; import chalk from 'chalk'; @@ -48,8 +49,10 @@ export default class CustomConsole extends Console { } assert(...args: Array) { - if (args[0]) { - this._log('assert', format(...args.slice(1))); + try { + assert(...args); + } catch (error) { + this._log('assert', error.toString()); } } diff --git a/packages/jest-util/src/__tests__/buffered_console.test.js b/packages/jest-util/src/__tests__/buffered_console.test.js index bbae95c8c578..103b9091a81b 100644 --- a/packages/jest-util/src/__tests__/buffered_console.test.js +++ b/packages/jest-util/src/__tests__/buffered_console.test.js @@ -23,16 +23,30 @@ describe('CustomConsole', () => { }); describe('assert', () => { - test('log when the assertion is truthy', () => { + test('do not log when the assertion is truthy', () => { + _console.assert(true); + + expect(stdout()).toMatch(''); + }); + + test('do not log when the assertion is truthy and there is a message', () => { _console.assert(true, 'ok'); - expect(stdout()).toMatch('ok'); + expect(stdout()).toMatch(''); }); - test('do not log when the assertion is falsy', () => { + test('log the assertion error when the assertion is falsy', () => { + _console.assert(false); + + expect(stdout()).toMatch('AssertionError'); + expect(stdout()).toMatch('false == true'); + }); + + test('log the assertion error when the assertion is falsy with another message argument', () => { _console.assert(false, 'ok'); - expect(stdout()).toEqual(''); + expect(stdout()).toMatch('AssertionError'); + expect(stdout()).toMatch('ok'); }); }); diff --git a/packages/jest-util/src/__tests__/console.test.js b/packages/jest-util/src/__tests__/console.test.js index d42203e29271..4502f8ac32f6 100644 --- a/packages/jest-util/src/__tests__/console.test.js +++ b/packages/jest-util/src/__tests__/console.test.js @@ -24,16 +24,30 @@ describe('CustomConsole', () => { }); describe('assert', () => { - test('log when the assertion is truthy', () => { + test('do not log when the assertion is truthy', () => { + _console.assert(true); + + expect(_stdout).toMatch(''); + }); + + test('do not log when the assertion is truthy and there is a message', () => { _console.assert(true, 'ok'); - expect(_stdout).toMatch('ok'); + expect(_stdout).toMatch(''); }); - test('do not log when the assertion is falsy', () => { + test('log the assertion error when the assertion is falsy', () => { + _console.assert(false); + + expect(_stdout).toMatch('AssertionError'); + expect(_stdout).toMatch('false == true'); + }); + + test('log the assertion error when the assertion is falsy with another message argument', () => { _console.assert(false, 'ok'); - expect(_stdout).toEqual(''); + expect(_stdout).toMatch('AssertionError'); + expect(_stdout).toMatch('ok'); }); }); diff --git a/packages/jest-util/src/buffered_console.js b/packages/jest-util/src/buffered_console.js index 1a563b500ca2..7c0ddca9f73d 100644 --- a/packages/jest-util/src/buffered_console.js +++ b/packages/jest-util/src/buffered_console.js @@ -15,6 +15,7 @@ import type { LogTimers, } from 'types/Console'; +import assert from 'assert'; import {Console} from 'console'; import {format} from 'util'; import chalk from 'chalk'; @@ -63,8 +64,10 @@ export default class BufferedConsole extends Console { } assert(...args: Array) { - if (args[0]) { - this._log('assert', format(...args.slice(1))); + try { + assert(...args); + } catch (error) { + this._log('assert', error.toString()); } }