Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix console & buffered console assert behaviour #5576

Merged
merged 4 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct in node env, not in jsdom. Unsure if we care about that differentiation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The differences are small, but I'm not sure it should affect this fix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node:

image

Chrome:
image

The output is pretty different

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB If I understand correctly, jsdom forwards its output to the Node.js console by default.

I'm not sure that we should print different messages (AssertionError [ERR_ASSERTION]/Assertion failed) depends on the environment.

What do you suggest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer thoughts?

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