Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when using debug to print to console, output is missing on reruns #1768

Closed
6 tasks done
panick-carousel opened this issue Aug 2, 2022 · 7 comments
Closed
6 tasks done

Comments

@panick-carousel
Copy link

Describe the bug

I am using the debug tool with vitest and cannot reliably get output from this tool. It appears as tho when in watch mode, vitest clears the console and wipes any debug output under some circumstances.

the debug library writes to stderr rather than stdout, so perhaps the clearing code in vite should account for lines due to stderr somehow ?

Thank you kindly for vitest - it makes life so much better.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-qiyvqq?file=test%2Fsuite.test.ts,package.json&initialPath=__vitest__

In terminal, press enter to trigger test reruns and observe the output to console from the debug() library.

Then start editing any file to trigger a test rerun, and observe the output to console from debug() is removed.

System Info

System:
    OS: Linux 5.17 Fedora Linux 36 (Server Edition)
    CPU: (16) x64 Intel(R) Xeon(R) E-2286M  CPU @ 2.40GHz
    Memory: 14.55 GB / 19.53 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.6.0 - /run/user/1001/fnm_multishells/788267_1659397396823/bin/node
    Yarn: 1.22.19 - /run/user/1001/fnm_multishells/788267_1659397396823/bin/yarn
    npm: 8.13.2 - /run/user/1001/fnm_multishells/788267_1659397396823/bin/npm

Used Package Manager

yarn

Validations

@panick-carousel
Copy link
Author

I managed to make the logging from debug be more usable by writing to stdout instead of stderr: debug.log = console.log.bind(console) but even that still fails to display the output roughly 1 in 20 runs.

@jcalfee
Copy link

jcalfee commented Mar 18, 2023

It may be related to length. By trading streams perhaps it is just giving it a little more time before it fills up again. #2671 (comment)

@inverted-capital
Copy link

Turns out, the issue is to do with the spinner used by the default test reporter. If the default test reporter is changed to 'dot' or any other one that doesn't have a spinner in it, the output is preserved with no issue.

https://vitest.dev/config/#reporters

@inverted-capital
Copy link

inverted-capital commented Jan 11, 2024

actually even 'dot' does some clearing of the terminal, so for #2671 when only a single character is being printed to stderr, it is only visible using a reporter than does not change any existing output on the terminal. Currently the 'tap' reporter is working for even a single character output.

@aleclarson
Copy link
Contributor

aleclarson commented Oct 31, 2024

Here's the workaround I'm using.

import createDebug from 'debug'

export let debug = createDebug('foo')

if (process.env.TEST && debug.enabled) {
  debug = console.log.bind(console) as any
  debug.enabled = true
}

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 1, 2024

This is because debug package directly writes to process.stderr by default https://github.com/debug-js/debug/blob/bc60914816e5e45a5fff1cd638410438fc317521/src/node.js#L194 while Vitest only captures global console logging.

I think this is working intended and it's necessary to override debug.log like suggested above for this case. Thanks people for sharing workaround 🙏

Just one more idea is that it looks like it's possible to overriding the default export directly from setup files. I'll leave a latest repro here https://stackblitz.com/edit/vitest-dev-vitest-mjacyu?file=test%2Fsetup.ts and let me close the issue as it's working intended.

import createDebug from 'debug';
createDebug.log = console.error.bind(console);
 DEV  v2.1.4 /home/projects/vitest-dev-vitest-mjacyu

stderr | test/basic.test.ts > foo
  repro meow1 +0ms
  repro meow2 +1ms

stderr | test/basic.test.ts > bar
  repro xxx +0ms

 ✓ test/basic.test.ts (2)
   ✓ foo
   ✓ bar

@hi-ogawa hi-ogawa closed this as completed Nov 1, 2024
@AriPerkkio
Copy link
Member

AriPerkkio commented Nov 1, 2024

I see couple of issues here:

  1. The forks pool is currently unable to pass direct process.stdout to Vitest. This will be fixed by fix(child_process): pipe stdout and stderr to main thread tinylibs/tinypool#104
  2. Vitest's reporters using log-update are currently erasing all process.stdout writes. To users this makes the output flicker as it's conflicting with our "internal write buffer". There's on-going work on rewriting Vitest's test reporters that will solve this. We need to intercept global process.stdout and handle that properly in CLI output.

Using the createDebug.log = console.error.bind(console); work-around mentioned above looks like good solution for now.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants