Skip to content

Commit

Permalink
test: add tests for console.[info|error|warn]
Browse files Browse the repository at this point in the history
Just copied the basic tests for log, as they're all the same thing
as log in either stdout or stderr. Cleaned that up a bit.

Also const-ified.

PR-URL: #6538
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bengl authored and evanlucas committed May 17, 2016
1 parent 83dab80 commit 8141c2f
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ assert.doesNotThrow(function() {
});

// an Object with a custom .inspect() function
var custom_inspect = { foo: 'bar', inspect: function() { return 'inspect'; } };
const custom_inspect = { foo: 'bar', inspect: () => { return 'inspect'; } };

var stdout_write = global.process.stdout.write;
var strings = [];
const stdout_write = global.process.stdout.write;
const stderr_write = global.process.stderr.write;
const strings = [];
const errStrings = [];
global.process.stdout.write = function(string) {
strings.push(string);
};
console._stderr = process.stdout;
global.process.stderr.write = function(string) {
errStrings.push(string);
};

// test console.log()
console.log('foo');
Expand All @@ -38,6 +42,27 @@ console.log('%s %s', 'foo', 'bar', 'hop');
console.log({slashes: '\\\\'});
console.log(custom_inspect);

// test console.info()
console.info('foo');
console.info('foo', 'bar');
console.info('%s %s', 'foo', 'bar', 'hop');
console.info({slashes: '\\\\'});
console.info(custom_inspect);

// test console.error()
console.error('foo');
console.error('foo', 'bar');
console.error('%s %s', 'foo', 'bar', 'hop');
console.error({slashes: '\\\\'});
console.error(custom_inspect);

// test console.warn()
console.warn('foo');
console.warn('foo', 'bar');
console.warn('%s %s', 'foo', 'bar', 'hop');
console.warn({slashes: '\\\\'});
console.warn(custom_inspect);

// test console.dir()
console.dir(custom_inspect);
console.dir(custom_inspect, { showHidden: false });
Expand All @@ -60,6 +85,7 @@ console.time('hasOwnProperty');
console.timeEnd('hasOwnProperty');

global.process.stdout.write = stdout_write;
global.process.stderr.write = stderr_write;

// verify that console.timeEnd() doesn't leave dead links
const timesMapSize = console._times.size;
Expand All @@ -71,22 +97,34 @@ console.timeEnd('label2');
console.timeEnd('label3');
assert.strictEqual(console._times.size, timesMapSize);

assert.equal('foo\n', strings.shift());
assert.equal('foo bar\n', strings.shift());
assert.equal('foo bar hop\n', strings.shift());
assert.equal("{ slashes: '\\\\\\\\' }\n", strings.shift());
assert.equal('inspect\n', strings.shift());
const expectedStrings = [
'foo', 'foo bar', 'foo bar hop', "{ slashes: '\\\\\\\\' }", 'inspect'
];

for (const expected of expectedStrings) {
assert.equal(expected + '\n', strings.shift()); // console.log (stdout)
assert.equal(expected + '\n', errStrings.shift()); // console.error (stderr)
}

for (const expected of expectedStrings) {
assert.equal(expected + '\n', strings.shift()); // console.info (stdout)
assert.equal(expected + '\n', errStrings.shift()); // console.warn (stderr)
}

assert.equal("{ foo: 'bar', inspect: [Function] }\n", strings.shift());
assert.equal("{ foo: 'bar', inspect: [Function] }\n", strings.shift());
assert.notEqual(-1, strings.shift().indexOf('foo: [Object]'));
assert.equal(-1, strings.shift().indexOf('baz'));
assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
strings.shift().split('\n').shift());
assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim()));
assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));

assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
errStrings.shift().split('\n').shift());

assert.equal(strings.length, 0);
assert.equal(errStrings.length, 0);

assert.throws(() => {
console.assert(false, 'should throw');
Expand Down

0 comments on commit 8141c2f

Please sign in to comment.