import { describe, expect, it, vi } from 'vitest' import type { Plugin } from 'vite' import type { Component } from '@nuxt/schema' import type { UnpluginOptions } from 'unplugin' import { islandsTransform } from '../src/components/islandsTransform' import { normalizeLineEndings } from './utils' const getComponents = () => [{ filePath: '/root/hello.server.vue', mode: 'server', pascalName: 'HelloWorld', island: true, kebabName: 'hello-world', chunkName: 'components/hello-world', export: 'default', shortPath: '', prefetch: false, preload: false }] as Component[] const pluginVite = islandsTransform.raw({ getComponents }, { framework: 'vite' }) as Plugin const pluginViteDev = islandsTransform.raw({ getComponents, rootDir: '/root', isDev: true }, { framework: 'vite' }) as Plugin const pluginWebpack = islandsTransform.raw({ getComponents }, { framework: 'webpack', webpack: { compiler: {} as any } }) const viteTransform = async (source: string, id: string, dev = false) => { const result = await ((dev ? pluginViteDev : pluginVite).transform! as Function)(source, id) return typeof result === 'string' ? result : result?.code } const webpackTransform = async (source: string, id: string) => { const result = await ((pluginWebpack as UnpluginOptions).transform! as Function)(source, id) return typeof result === 'string' ? result : result?.code } describe('islandTransform - server and island components', () => { describe('slots', () => { it('expect slot transform to match inline snapshot', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('expect slot fallback transform to match inline snapshot', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) }) describe('nuxt-client', () => { it('test transform with vite', async () => { const result = await viteTransform(` `, 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('test transform with vite in dev', async () => { const result = await viteTransform(` `, 'hello.server.vue', true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('test transform with webpack', async () => { const spyOnWarn = vi.spyOn(console, 'warn') const result = await webpackTransform(` `, 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) expect(spyOnWarn).toHaveBeenCalledWith('nuxt-client attribute and client components within islands is only supported in vite mode. file: hello.server.vue') }) }) })