Skip to content

Commit

Permalink
fix console & buffered console assert behaviour (#5576)
Browse files Browse the repository at this point in the history
* fix console & buffered console assert behaviour

* add console assert fix to changelog

* fix tests to match two different assertion error style in node 6 and above

* refactor arguments passed from console.assert to the assert module
  • Loading branch information
ranyitz authored and cpojer committed Feb 19, 2018
1 parent adbd927 commit 593d801
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions packages/jest-util/src/Console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -48,8 +49,10 @@ export default class CustomConsole extends Console {
}

assert(...args: Array<any>) {
if (args[0]) {
this._log('assert', format(...args.slice(1)));
try {
assert(...args);
} catch (error) {
this._log('assert', error.toString());
}
}

Expand Down
22 changes: 18 additions & 4 deletions packages/jest-util/src/__tests__/buffered_console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
22 changes: 18 additions & 4 deletions packages/jest-util/src/__tests__/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
7 changes: 5 additions & 2 deletions packages/jest-util/src/buffered_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,8 +64,10 @@ export default class BufferedConsole extends Console {
}

assert(...args: Array<any>) {
if (args[0]) {
this._log('assert', format(...args.slice(1)));
try {
assert(...args);
} catch (error) {
this._log('assert', error.toString());
}
}

Expand Down

0 comments on commit 593d801

Please sign in to comment.