Skip to content

Commit cb17486

Browse files
committed
fix console & buffered console assert behaviour
1 parent fcdf071 commit cb17486

4 files changed

Lines changed: 44 additions & 12 deletions

File tree

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[0], ...args.slice(1));
54+
} catch (error) {
55+
this._log('assert', error.toString());
5356
}
5457
}
5558

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,28 @@ 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('');
36+
});
37+
38+
test('log the assertion error when the assertion is falsy', () => {
39+
_console.assert(false);
40+
41+
expect(stdout()).toEqual('AssertionError [ERR_ASSERTION]: false == true');
3042
});
3143

32-
test('do not log when the assertion is falsy', () => {
44+
test('log the assertion error when the assertion is falsy with another message argument', () => {
3345
_console.assert(false, 'ok');
3446

35-
expect(stdout()).toEqual('');
47+
expect(stdout()).toEqual('AssertionError [ERR_ASSERTION]: ok');
3648
});
3749
});
3850

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('');
37+
});
38+
39+
test('log the assertion error when the assertion is falsy', () => {
40+
_console.assert(false);
41+
42+
expect(_stdout).toEqual(
43+
'AssertionError [ERR_ASSERTION]: false == true\n',
44+
);
3145
});
3246

33-
test('do not log when the assertion is falsy', () => {
47+
test('log the assertion error when the assertion is falsy with another message argument', () => {
3448
_console.assert(false, 'ok');
3549

36-
expect(_stdout).toEqual('');
50+
expect(_stdout).toEqual('AssertionError [ERR_ASSERTION]: ok\n');
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[0], ...args.slice(1));
69+
} catch (error) {
70+
this._log('assert', error.toString());
6871
}
6972
}
7073

0 commit comments

Comments
 (0)