architect / tap-arc

Node.js spec-like TAP reporter
https://www.npmjs.com/package/tap-arc
Apache License 2.0
30 stars 3 forks source link

Preserve whitespace in console.log statements #16

Closed tbeseda closed 1 year ago

tbeseda commented 2 years ago

It would be nice to preserve whitespace in multi-line logs that occur in the middle of tests.

For example, the reporter should print the console.log statement that spans multiple lines with empty lines

test('logging inside a test', function (t) {
  console.log('Logging from a test can be helpful')
  console.log('\u001B[?25l') // should not print
  console.log(`
Multi-line comments
are also

really

cool.
  `)

  t.end()
})
tbeseda commented 2 years ago

I toyed around with preserving whitespace in console.logs in tap-arc. Ran into 2 problems The event the parser sends with non-TAP lines is extra. tap-parser does have a preserveWhitespace option :thumbsup: -- but extra doesn't get those empty lines

Another event does though! line gets almost all lines from the TAP output. so I attempted to filter out non-empty chunks from the line event and print those. Unfortunately, the order isn't guaranteed (the nature of streams), so the blank lines end up being emitted before non-blank lines in a console log

Changing tap-arc to listen to line like this where I intentionally add a $ to call out lines printed from line event

parser.on('line', (line) => {
  if (line.trim().length === 0) print('$' + line)
})

parser.on('extra', (extra) => {
  const stripped = stripAnsi(extra).trim()
  const justAnsi = stripped.length === 0 && extra.length > 0
  if (!justAnsi) print(`${pad(2)}${extra}`)
})

gives us logs like this

  basic arithmetic without messages
    ✔ should be strictly equal
$
$
$
    ✖ 2) should be strictly equal
      Expected 666 but got 66
      At: Test.<anonymous> (/test/create-mixed-tap.js:5:5)

  logging inside a test
    Logging from a test can be helpful
    Multi-line comments
    are also
    really
    cool.
$  

You can see that the empty lines from the "logging inside a test" are actually parsed and printed way before the others, in the middle of the previous test group