|
1 | 1 | 'use strict'; |
2 | | -require('../common'); |
| 2 | +const common = require('../common'); |
3 | 3 | const assert = require('assert'); |
4 | 4 | const Stream = require('stream'); |
5 | 5 | const Console = require('console').Console; |
6 | | -let called = false; |
7 | 6 |
|
8 | 7 | const out = new Stream(); |
9 | 8 | const err = new Stream(); |
10 | 9 |
|
11 | 10 | // ensure the Console instance doesn't write to the |
12 | 11 | // 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; |
16 | 13 |
|
17 | 14 | // make sure that the "Console" function exists |
18 | 15 | assert.strictEqual('function', typeof Console); |
19 | 16 |
|
20 | 17 | // make sure that the Console constructor throws |
21 | 18 | // when not given a writable stream instance |
22 | | -assert.throws(function() { |
| 19 | +assert.throws(() => { |
23 | 20 | new Console(); |
24 | | -}, /Console expects a writable stream/); |
| 21 | +}, /^TypeError: Console expects a writable stream instance$/); |
25 | 22 |
|
26 | 23 | // 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 = () => {}; |
29 | 26 | err.write = undefined; |
30 | 27 | new Console(out, err); |
31 | | -}, /Console expects writable stream instances/); |
| 28 | +}, /^TypeError: Console expects writable stream instances$/); |
32 | 29 |
|
33 | | -out.write = err.write = function(d) {}; |
| 30 | +out.write = err.write = (d) => {}; |
34 | 31 |
|
35 | 32 | const c = new Console(out, err); |
36 | 33 |
|
37 | | -out.write = err.write = function(d) { |
| 34 | +out.write = err.write = common.mustCall((d) => { |
38 | 35 | assert.strictEqual(d, 'test\n'); |
39 | | - called = true; |
40 | | -}; |
| 36 | +}, 2); |
41 | 37 |
|
42 | | -assert(!called); |
43 | 38 | c.log('test'); |
44 | | -assert(called); |
45 | | - |
46 | | -called = false; |
47 | 39 | c.error('test'); |
48 | | -assert(called); |
49 | 40 |
|
50 | | -out.write = function(d) { |
| 41 | +out.write = common.mustCall((d) => { |
51 | 42 | assert.strictEqual('{ foo: 1 }\n', d); |
52 | | - called = true; |
53 | | -}; |
| 43 | +}); |
54 | 44 |
|
55 | | -called = false; |
56 | 45 | c.dir({ foo: 1 }); |
57 | | -assert(called); |
58 | 46 |
|
59 | 47 | // 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) => { |
62 | 50 | 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 | + |
65 | 54 | [1, 2, 3].forEach(c.log); |
66 | | -assert.strictEqual(3, called); |
67 | 55 |
|
68 | 56 | // Console() detects if it is called without `new` keyword |
69 | | -assert.doesNotThrow(function() { |
| 57 | +assert.doesNotThrow(() => { |
70 | 58 | Console(out, err); |
71 | 59 | }); |
0 commit comments