Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log() in VS Code with vitest and vitest test runner

I'm using vitest in VS Code with the official Vitest test runner plugin.

All works as expected, but any console.log() statements don't seem to appear anywhere.

Not in the VScode test results tab, or the test output tab, not in the Testing panel...

I've also tried importing log directly, same result (no output):

import { log } from "console";

Am I missing a configuration option?

like image 472
Kong Avatar asked Feb 28 '26 10:02

Kong


2 Answers

In my case what this the trick was:

vitest run --printConsoleTrace=true --silent=false

You can check it out here https://vitest.dev/guide/cli#printconsoletrace

like image 114
Dany Rasho Avatar answered Mar 02 '26 13:03

Dany Rasho


Actually, I'm facing an identical issue, and although I haven't been able to definitively resolve it, here are a few things that might help:

  1. Make sure that if your tests call any asynchronous functions, that the test functions themselves (describe, it('should do something')) are defined as async. For example:
    describe('my tested module', () => {...} // INCORRECT

    describe('my tested module', async () => {...} // CORRECT
  1. In the Vitest configuration file (e.g. vitests.config.ts), inside the test object in the call to defineConfig(), you are not defining onConsoleLog handler that returns false. This will stop Vitest from outputting console.log() statements to the debug console. See documentation on Configuring Vitest.

Example:

    export default defineConfig({
      test: {
        server: { deps: { inline: ['@fastify/autoload'] } },
        testTimeout: 100000,
        setupFiles: ['dotenv/config'],
        onConsoleLog(log: string, type: 'stdout' | 'stderr'): boolean | void {
          return false;  // NO console.log() statements will be printed!
        },
      },
    });
  1. Interestingly enough, running the tests using the Vitest extension does result in the console.log() statements being printed to the console, while debugging them does not. I am not sure why that is.
like image 22
Maya Avatar answered Mar 02 '26 15:03

Maya