Skip to content

Commit

Permalink
test: fix test (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Apr 27, 2023
1 parent ee83cea commit 55484ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
25 changes: 15 additions & 10 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import('consola')>('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({
Expand All @@ -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', () => {
Expand Down
20 changes: 10 additions & 10 deletions test/configs.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import('consola')>('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({
Expand All @@ -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', () => {
Expand Down
3 changes: 1 addition & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable spaced-comment */
/// <reference types="vitest" />
/// <reference types="vitest/globals" />

import { resolve } from 'path'
import { defineConfig } from 'vite'
import { defineConfig } from 'vitest/config'

export default defineConfig({
resolve: {
Expand Down

0 comments on commit 55484ca

Please sign in to comment.