From aae274334b48dcdc5b0750dfbc125ce615ad8745 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 27 Apr 2023 12:22:05 +0200 Subject: [PATCH] test: fix test --- test/basic.test.ts | 25 +++++++++++++++---------- test/configs.test.ts | 20 ++++++++++---------- vitest.config.ts | 3 +-- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/test/basic.test.ts b/test/basic.test.ts index 95dceb9d..3e7a9920 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -1,14 +1,17 @@ -import { useTestContext, $fetch } from '@nuxt/test-utils' -import { describe, test, expect, vi } from 'vitest' -import { mockedInfo } from 'consola' +import { useTestContext } from '@nuxt/test-utils' +import { describe, test, expect, vi, afterAll } from 'vitest' import { r, setupNuxtTailwind } from './util' describe('tailwindcss module', async () => { - vi.mock('consola', async () => { - const { default: mod } = (await vi.importActual('consola')) - mod.withScope = () => mod - mod.info = vi.fn() - return { default: mod, mockedInfo: mod.info } + // Consola will by default set the log level to warn in test, we trick it into thinking we're in debug mode + process.env.DEBUG = 'nuxt:*' + + const spyStderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => undefined!) + const spyStdout = vi.spyOn(process.stdout, 'write').mockImplementation(() => undefined!) + + afterAll(() => { + spyStderr.mockRestore() + spyStdout.mockRestore() }) await setupNuxtTailwind({ @@ -33,10 +36,12 @@ describe('tailwindcss module', async () => { test('include custom tailwind.css file in project css', () => { const nuxt = useTestContext().nuxt - expect(mockedInfo.mock.calls[0]).contains('Using Tailwind CSS from ~/tailwind.css') - expect(nuxt.options.css).toHaveLength(1) expect(nuxt.options.css[0]).toEqual(nuxt.options.tailwindcss.cssPath) + + expect(spyStderr).toHaveBeenCalledTimes(0) + expect(spyStdout).toHaveBeenCalledTimes(1) + expect(spyStdout.mock.calls[0][0]).contains('Using Tailwind CSS from ~/tailwind.css') }) test('default js config is merged in', () => { diff --git a/test/configs.test.ts b/test/configs.test.ts index ff224c26..fdbb75e0 100644 --- a/test/configs.test.ts +++ b/test/configs.test.ts @@ -1,16 +1,15 @@ import { describe, test, expect, vi } from 'vitest' -import { mockedWarn } from 'consola' import { useTestContext } from '@nuxt/test-utils' import destr from 'destr' import { setupNuxtTailwind } from './util' describe('tailwindcss module configs', async () => { - vi.mock('consola', async () => { - const { default: mod } = (await vi.importActual('consola')) - mod.withScope = () => mod - mod.info = vi.fn() - mod.warn = vi.fn() - return { default: mod, info: mod.info, mockedWarn: mod.warn } + const spyStderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => undefined!) + const spyStdout = vi.spyOn(process.stdout, 'write').mockImplementation(() => undefined!) + + afterAll(() => { + spyStderr.mockRestore() + spyStdout.mockRestore() }) await setupNuxtTailwind({ @@ -25,10 +24,11 @@ describe('tailwindcss module configs', async () => { }) test('throws error about malformed config', () => { - expect(mockedWarn.mock.calls[0][0]).toMatchInlineSnapshot('"Failed to load Tailwind config at: `./malformed-tailwind.config.js`"') - expect(mockedWarn.mock.calls[0][0]).contains('Failed to load Tailwind config at: `./malformed-tailwind.config.js`') + expect(spyStderr).toBeCalledTimes(1) + const output = spyStderr.mock.calls[0][0].toString() - expect(mockedWarn.mock.calls[0].length).toBe(2) + expect(output.split('\n')[0]).toMatchInlineSnapshot('"[warn] [nuxt:tailwindcss] Failed to load Tailwind config at: `./malformed-tailwind.config.js` Cannot find module \'something-that-doesnt-exist\'"') + expect(output).contains('Failed to load Tailwind config at: `./malformed-tailwind.config.js`') }) test('ts config file is loaded and merged', () => { diff --git a/vitest.config.ts b/vitest.config.ts index 2eb12407..8f452813 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,9 +1,8 @@ -/* eslint-disable spaced-comment */ /// /// import { resolve } from 'path' -import { defineConfig } from 'vite' +import { defineConfig } from 'vitest/config' export default defineConfig({ resolve: {