test-utils icon indicating copy to clipboard operation
test-utils copied to clipboard

@nuxt/test-utils -- setup() clobbers console.log() functionality

Open dhait opened this issue 2 years ago • 3 comments

Environment

  • Operating System: Darwin
  • Node Version: v18.16.0
  • Nuxt Version: 3.5.2
  • Nitro Version: 2.4.1
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-kp3tnj?file=test%2Fmyhello.test.ts

Describe the bug

When setup() is called at the beginning of a vitest test file (as described in the docs https://nuxt.com/docs/getting-started/testing), console.log() no longer displays text on the console during the test run.

When setup() is commented out, console.log() works as expected.

Additional context

No response

Logs

No response

dhait avatar Jun 06 '23 13:06 dhait

Same here. would be great if we can have this working. It makes hard to write simple testing :s

flozero avatar Jul 30 '23 12:07 flozero

I ran into the same issue, not sure how to fix it but I do have a work around to be able to log still. Assigning console.log to a variable before running setup allows you to use it for logging.

import { describe, test } from "vitest";
import { setup } from "@nuxt/test-utils";

// assign console.log before setup
const logger = console.log;
await setup({});

describe("My tests", () => {
    test("a test", () => {
        logger("hello there");
    });
});

BobbieGoede avatar Aug 05 '23 11:08 BobbieGoede

I found another workaround, it seems console.log is being wrapped by consola at some point. Calling consola.restoreConsole in beforeEach allows you to use console.log as normal, not sure if there are any drawbacks to this.

import { consola } from 'consola'

beforeEach(() => {
  consola.restoreConsole()
})

await setup({});

describe("My tests", () => {
    test("a test", () => {
        console.log("hello there");
    });
});

BobbieGoede avatar Aug 06 '23 10:08 BobbieGoede