Kong
Kong

Reputation: 9586

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?

Upvotes: 12

Views: 7887

Answers (4)

Dany Rasho
Dany Rasho

Reputation: 11

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

Upvotes: 1

user369662
user369662

Reputation: 1

It helped me --disable-console-intercept
Source: https://github.com/vitest-dev/vscode/discussions/117

Upvotes: -1

Maya
Maya

Reputation: 137

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.

Upvotes: 1

Stephen
Stephen

Reputation: 1416

I had a similar issue where console.logs weren't always in the test output with Vitest. In my case the issue was not awaiting an async function I called in a test. I didn't need the return value so I didn't think it would matter, but it's important for the test runner to wait until every function it runs is actually finished.

Upvotes: 1

Related Questions