-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add methods to custom and buffered consoles (#5514)
* add methods to custom and buffered consoles * update changelog with the console methods addition * use spyOn console to mock the implementation * refactor console's calls to use ...args instead of arguments
- Loading branch information
Showing
6 changed files
with
493 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/jest-util/src/__tests__/buffered_console.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import chalk from 'chalk'; | ||
import BufferedConsole from '../buffered_console'; | ||
|
||
describe('CustomConsole', () => { | ||
let _console; | ||
const stdout = () => | ||
_console | ||
.getBuffer() | ||
.map(log => log.message) | ||
.join('\n'); | ||
|
||
beforeEach(() => { | ||
_console = new BufferedConsole(); | ||
}); | ||
|
||
describe('assert', () => { | ||
test('log when the assertion is truthy', () => { | ||
_console.assert(true, 'ok'); | ||
|
||
expect(stdout()).toMatch('ok'); | ||
}); | ||
|
||
test('do not log when the assertion is falsy', () => { | ||
_console.assert(false, 'ok'); | ||
|
||
expect(stdout()).toEqual(''); | ||
}); | ||
}); | ||
|
||
describe('count', () => { | ||
test('count using the default counter', () => { | ||
_console.count(); | ||
_console.count(); | ||
_console.count(); | ||
|
||
expect(stdout()).toEqual('default: 1\ndefault: 2\ndefault: 3'); | ||
}); | ||
|
||
test('count using the a labeled counter', () => { | ||
_console.count('custom'); | ||
_console.count('custom'); | ||
_console.count('custom'); | ||
|
||
expect(stdout()).toEqual('custom: 1\ncustom: 2\ncustom: 3'); | ||
}); | ||
|
||
test('countReset restarts default counter', () => { | ||
_console.count(); | ||
_console.count(); | ||
_console.countReset(); | ||
_console.count(); | ||
expect(stdout()).toEqual('default: 1\ndefault: 2\ndefault: 1'); | ||
}); | ||
|
||
test('countReset restarts custom counter', () => { | ||
_console.count('custom'); | ||
_console.count('custom'); | ||
_console.countReset('custom'); | ||
_console.count('custom'); | ||
|
||
expect(stdout()).toEqual('custom: 1\ncustom: 2\ncustom: 1'); | ||
}); | ||
}); | ||
|
||
describe('group', () => { | ||
test('group without label', () => { | ||
_console.group(); | ||
_console.log('hey'); | ||
_console.group(); | ||
_console.log('there'); | ||
|
||
expect(stdout()).toEqual(' hey\n there'); | ||
}); | ||
|
||
test('group with label', () => { | ||
_console.group('first'); | ||
_console.log('hey'); | ||
_console.group('second'); | ||
_console.log('there'); | ||
|
||
expect(stdout()).toEqual(` ${chalk.bold('first')} | ||
hey | ||
${chalk.bold('second')} | ||
there`); | ||
}); | ||
|
||
test('groupEnd remove the indentation of the current group', () => { | ||
_console.group(); | ||
_console.log('hey'); | ||
_console.groupEnd(); | ||
_console.log('there'); | ||
|
||
expect(stdout()).toEqual(' hey\nthere'); | ||
}); | ||
|
||
test('groupEnd can not remove the indentation below the starting point', () => { | ||
_console.groupEnd(); | ||
_console.groupEnd(); | ||
_console.group(); | ||
_console.log('hey'); | ||
_console.groupEnd(); | ||
_console.log('there'); | ||
|
||
expect(stdout()).toEqual(' hey\nthere'); | ||
}); | ||
}); | ||
|
||
describe('time', () => { | ||
test('should return the time between time() and timeEnd() on default timer', () => { | ||
_console.time(); | ||
_console.timeEnd(); | ||
|
||
expect(stdout()).toMatch('default: '); | ||
expect(stdout()).toMatch('ms'); | ||
}); | ||
|
||
test('should return the time between time() and timeEnd() on custom timer', () => { | ||
_console.time('custom'); | ||
_console.timeEnd('custom'); | ||
|
||
expect(stdout()).toMatch('custom: '); | ||
expect(stdout()).toMatch('ms'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.