Skip to content

Commit 593d801

Browse files
ranyitzcpojer
authored andcommitted
fix console & buffered console assert behaviour (#5576)
* 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
1 parent adbd927 commit 593d801

5 files changed

Lines changed: 48 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
([#5560](https://github.com/facebook/jest/pull/5560))
1414
* `[jest-config]` Make it possible to merge `transform` option with preset
1515
([#5505](https://github.com/facebook/jest/pull/5505))
16+
* `[jest-util]` Fix `console.assert` behavior in custom & buffered consoles
17+
([#5576](https://github.com/facebook/jest/pull/5576))
1618

1719
### Features
1820

packages/jest-util/src/Console.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import type {LogType, LogMessage, LogCounters, LogTimers} from 'types/Console';
1212

13+
import assert from 'assert';
1314
import {format} from 'util';
1415
import {Console} from 'console';
1516
import chalk from 'chalk';
@@ -48,8 +49,10 @@ export default class CustomConsole extends Console {
4849
}
4950

5051
assert(...args: Array<any>) {
51-
if (args[0]) {
52-
this._log('assert', format(...args.slice(1)));
52+
try {
53+
assert(...args);
54+
} catch (error) {
55+
this._log('assert', error.toString());
5356
}
5457
}
5558

packages/jest-util/src/__tests__/buffered_console.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,30 @@ describe('CustomConsole', () => {
2323
});
2424

2525
describe('assert', () => {
26-
test('log when the assertion is truthy', () => {
26+
test('do not log when the assertion is truthy', () => {
27+
_console.assert(true);
28+
29+
expect(stdout()).toMatch('');
30+
});
31+
32+
test('do not log when the assertion is truthy and there is a message', () => {
2733
_console.assert(true, 'ok');
2834

29-
expect(stdout()).toMatch('ok');
35+
expect(stdout()).toMatch('');
3036
});
3137

32-
test('do not log when the assertion is falsy', () => {
38+
test('log the assertion error when the assertion is falsy', () => {
39+
_console.assert(false);
40+
41+
expect(stdout()).toMatch('AssertionError');
42+
expect(stdout()).toMatch('false == true');
43+
});
44+
45+
test('log the assertion error when the assertion is falsy with another message argument', () => {
3346
_console.assert(false, 'ok');
3447

35-
expect(stdout()).toEqual('');
48+
expect(stdout()).toMatch('AssertionError');
49+
expect(stdout()).toMatch('ok');
3650
});
3751
});
3852

packages/jest-util/src/__tests__/console.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@ describe('CustomConsole', () => {
2424
});
2525

2626
describe('assert', () => {
27-
test('log when the assertion is truthy', () => {
27+
test('do not log when the assertion is truthy', () => {
28+
_console.assert(true);
29+
30+
expect(_stdout).toMatch('');
31+
});
32+
33+
test('do not log when the assertion is truthy and there is a message', () => {
2834
_console.assert(true, 'ok');
2935

30-
expect(_stdout).toMatch('ok');
36+
expect(_stdout).toMatch('');
3137
});
3238

33-
test('do not log when the assertion is falsy', () => {
39+
test('log the assertion error when the assertion is falsy', () => {
40+
_console.assert(false);
41+
42+
expect(_stdout).toMatch('AssertionError');
43+
expect(_stdout).toMatch('false == true');
44+
});
45+
46+
test('log the assertion error when the assertion is falsy with another message argument', () => {
3447
_console.assert(false, 'ok');
3548

36-
expect(_stdout).toEqual('');
49+
expect(_stdout).toMatch('AssertionError');
50+
expect(_stdout).toMatch('ok');
3751
});
3852
});
3953

packages/jest-util/src/buffered_console.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
LogTimers,
1616
} from 'types/Console';
1717

18+
import assert from 'assert';
1819
import {Console} from 'console';
1920
import {format} from 'util';
2021
import chalk from 'chalk';
@@ -63,8 +64,10 @@ export default class BufferedConsole extends Console {
6364
}
6465

6566
assert(...args: Array<any>) {
66-
if (args[0]) {
67-
this._log('assert', format(...args.slice(1)));
67+
try {
68+
assert(...args);
69+
} catch (error) {
70+
this._log('assert', error.toString());
6871
}
6972
}
7073

0 commit comments

Comments
 (0)