Skip to content

Commit 8141c2f

Browse files
benglevanlucas
authored andcommitted
test: add tests for console.[info|error|warn]
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 <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 83dab80 commit 8141c2f

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

test/parallel/test-console.js

+49-11
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ assert.doesNotThrow(function() {
2222
});
2323

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

27-
var stdout_write = global.process.stdout.write;
28-
var strings = [];
27+
const stdout_write = global.process.stdout.write;
28+
const stderr_write = global.process.stderr.write;
29+
const strings = [];
30+
const errStrings = [];
2931
global.process.stdout.write = function(string) {
3032
strings.push(string);
3133
};
32-
console._stderr = process.stdout;
34+
global.process.stderr.write = function(string) {
35+
errStrings.push(string);
36+
};
3337

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

45+
// test console.info()
46+
console.info('foo');
47+
console.info('foo', 'bar');
48+
console.info('%s %s', 'foo', 'bar', 'hop');
49+
console.info({slashes: '\\\\'});
50+
console.info(custom_inspect);
51+
52+
// test console.error()
53+
console.error('foo');
54+
console.error('foo', 'bar');
55+
console.error('%s %s', 'foo', 'bar', 'hop');
56+
console.error({slashes: '\\\\'});
57+
console.error(custom_inspect);
58+
59+
// test console.warn()
60+
console.warn('foo');
61+
console.warn('foo', 'bar');
62+
console.warn('%s %s', 'foo', 'bar', 'hop');
63+
console.warn({slashes: '\\\\'});
64+
console.warn(custom_inspect);
65+
4166
// test console.dir()
4267
console.dir(custom_inspect);
4368
console.dir(custom_inspect, { showHidden: false });
@@ -60,6 +85,7 @@ console.time('hasOwnProperty');
6085
console.timeEnd('hasOwnProperty');
6186

6287
global.process.stdout.write = stdout_write;
88+
global.process.stderr.write = stderr_write;
6389

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

74-
assert.equal('foo\n', strings.shift());
75-
assert.equal('foo bar\n', strings.shift());
76-
assert.equal('foo bar hop\n', strings.shift());
77-
assert.equal("{ slashes: '\\\\\\\\' }\n", strings.shift());
78-
assert.equal('inspect\n', strings.shift());
100+
const expectedStrings = [
101+
'foo', 'foo bar', 'foo bar hop', "{ slashes: '\\\\\\\\' }", 'inspect'
102+
];
103+
104+
for (const expected of expectedStrings) {
105+
assert.equal(expected + '\n', strings.shift()); // console.log (stdout)
106+
assert.equal(expected + '\n', errStrings.shift()); // console.error (stderr)
107+
}
108+
109+
for (const expected of expectedStrings) {
110+
assert.equal(expected + '\n', strings.shift()); // console.info (stdout)
111+
assert.equal(expected + '\n', errStrings.shift()); // console.warn (stderr)
112+
}
113+
79114
assert.equal("{ foo: 'bar', inspect: [Function] }\n", strings.shift());
80115
assert.equal("{ foo: 'bar', inspect: [Function] }\n", strings.shift());
81116
assert.notEqual(-1, strings.shift().indexOf('foo: [Object]'));
82117
assert.equal(-1, strings.shift().indexOf('baz'));
83-
assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
84-
strings.shift().split('\n').shift());
85118
assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
86119
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
87120
assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim()));
88121
assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));
122+
123+
assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
124+
errStrings.shift().split('\n').shift());
125+
89126
assert.equal(strings.length, 0);
127+
assert.equal(errStrings.length, 0);
90128

91129
assert.throws(() => {
92130
console.assert(false, 'should throw');

0 commit comments

Comments
 (0)