Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1666600

Browse filesBrowse files
edsadrItalo A. Casas
authored andcommitted
test: improve code in test-console-instance
* use common.mustCall to validate functions executions * use common.fail to check test fail * improve error validations * remove unnecessary assertions * use arrow functions PR-URL: #10813 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent b496374 commit 1666600
Copy full SHA for 1666600

File tree

Expand file treeCollapse file tree

1 file changed

+18
-30
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-30
lines changed
Open diff view settings
Collapse file
+18-30Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,59 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const Stream = require('stream');
55
const Console = require('console').Console;
6-
let called = false;
76

87
const out = new Stream();
98
const err = new Stream();
109

1110
// ensure the Console instance doesn't write to the
1211
// process' "stdout" or "stderr" streams
13-
process.stdout.write = process.stderr.write = function() {
14-
throw new Error('write() should not be called!');
15-
};
12+
process.stdout.write = process.stderr.write = common.fail;
1613

1714
// make sure that the "Console" function exists
1815
assert.strictEqual('function', typeof Console);
1916

2017
// make sure that the Console constructor throws
2118
// when not given a writable stream instance
22-
assert.throws(function() {
19+
assert.throws(() => {
2320
new Console();
24-
}, /Console expects a writable stream/);
21+
}, /^TypeError: Console expects a writable stream instance$/);
2522

2623
// Console constructor should throw if stderr exists but is not writable
27-
assert.throws(function() {
28-
out.write = function() {};
24+
assert.throws(() => {
25+
out.write = () => {};
2926
err.write = undefined;
3027
new Console(out, err);
31-
}, /Console expects writable stream instances/);
28+
}, /^TypeError: Console expects writable stream instances$/);
3229

33-
out.write = err.write = function(d) {};
30+
out.write = err.write = (d) => {};
3431

3532
const c = new Console(out, err);
3633

37-
out.write = err.write = function(d) {
34+
out.write = err.write = common.mustCall((d) => {
3835
assert.strictEqual(d, 'test\n');
39-
called = true;
40-
};
36+
}, 2);
4137

42-
assert(!called);
4338
c.log('test');
44-
assert(called);
45-
46-
called = false;
4739
c.error('test');
48-
assert(called);
4940

50-
out.write = function(d) {
41+
out.write = common.mustCall((d) => {
5142
assert.strictEqual('{ foo: 1 }\n', d);
52-
called = true;
53-
};
43+
});
5444

55-
called = false;
5645
c.dir({ foo: 1 });
57-
assert(called);
5846

5947
// ensure that the console functions are bound to the console instance
60-
called = 0;
61-
out.write = function(d) {
48+
let called = 0;
49+
out.write = common.mustCall((d) => {
6250
called++;
63-
assert.strictEqual(d, called + ' ' + (called - 1) + ' [ 1, 2, 3 ]\n');
64-
};
51+
assert.strictEqual(d, `${called} ${called - 1} [ 1, 2, 3 ]\n`);
52+
}, 3);
53+
6554
[1, 2, 3].forEach(c.log);
66-
assert.strictEqual(3, called);
6755

6856
// Console() detects if it is called without `new` keyword
69-
assert.doesNotThrow(function() {
57+
assert.doesNotThrow(() => {
7058
Console(out, err);
7159
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.