-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
diagnostics_channel: capture console messages
- Loading branch information
Showing
3 changed files
with
151 additions
and
3 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
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,82 @@ | ||
'use strict'; | ||
|
||
const { mustCall } = require('../common'); | ||
const { deepStrictEqual, strictEqual, match, ok } = require('assert'); | ||
Check failure on line 4 in test/parallel/test-console-diagnostics-channels.js GitHub Actions / lint-js-and-md
|
||
const { formatWithOptions } = require('util'); | ||
|
||
const { channel } = require('diagnostics_channel'); | ||
|
||
const { | ||
hijackStdout, | ||
hijackStderr, | ||
restoreStdout, | ||
restoreStderr | ||
} = require('../common/hijackstdio'); | ||
|
||
const stdoutMethods = [ | ||
'log', | ||
'info', | ||
'debug' | ||
]; | ||
|
||
const stderrMethods = [ | ||
'warn', | ||
'error' | ||
]; | ||
|
||
const methods = [ | ||
...stdoutMethods, | ||
...stderrMethods | ||
]; | ||
|
||
const channels = { | ||
log: channel('console.log'), | ||
info: channel('console.info'), | ||
debug: channel('console.debug'), | ||
warn: channel('console.warn'), | ||
error: channel('console.error') | ||
} | ||
|
||
process.stdout.isTTY = false; | ||
process.stderr.isTTY = false; | ||
|
||
for (const method of methods) { | ||
let intercepted = false; | ||
let formatted = false; | ||
|
||
const hijack = stdoutMethods.includes(method) | ||
? hijackStdout | ||
: hijackStderr; | ||
|
||
const restore = stdoutMethods.includes(method) | ||
? restoreStdout | ||
: restoreStderr; | ||
|
||
const foo = 'string'; | ||
const bar = { key: /value/ }; | ||
const baz = [ 1, 2, 3 ]; | ||
|
||
channels[method].subscribe(mustCall((args) => { | ||
// Should not have been formatted yet. | ||
intercepted = true; | ||
ok(!formatted); | ||
|
||
// Should receive expected log message args. | ||
deepStrictEqual(args, [foo, bar, baz]); | ||
|
||
// Should be able to mutate message args and have it reflected in output. | ||
bar.added = true; | ||
})); | ||
|
||
hijack(mustCall((output) => { | ||
// Should have already been intercepted. | ||
formatted = true; | ||
ok(intercepted); | ||
|
||
// Should produce expected formatted output with mutated message args. | ||
deepStrictEqual(output, 'string { key: /value/, added: true } [ 1, 2, 3 ]\n'); | ||
})); | ||
|
||
console[method](foo, bar, baz); | ||
restore(); | ||
} |