From 26dff83a840d1062aea5ce3c4defb02648f87c85 Mon Sep 17 00:00:00 2001 From: julien huang Date: Sat, 19 Aug 2023 12:06:32 +0200 Subject: [PATCH] test: unit test for islandsTransform --- .../nuxt/src/components/islandsTransform.ts | 4 +- packages/nuxt/test/islandTransform.test.ts | 183 ++++++++++++++++++ 2 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 packages/nuxt/test/islandTransform.test.ts diff --git a/packages/nuxt/src/components/islandsTransform.ts b/packages/nuxt/src/components/islandsTransform.ts index a76a54089a..150de76fa0 100644 --- a/packages/nuxt/src/components/islandsTransform.ts +++ b/packages/nuxt/src/components/islandsTransform.ts @@ -25,7 +25,7 @@ interface ComponentChunkOptions { } const SCRIPT_RE = /]*>/g -const HAS_SLOT_RE = /([\s\S]*)<\/template>/ const NUXTCLIENT_ATTR_RE = /\snuxt-client(="[^"]*")?/g @@ -46,7 +46,7 @@ export const islandsTransform = createUnplugin((options: ServerOnlyComponentTran return islands.some(c => c.filePath === pathname) }, async transform (code, id) { - if (!HAS_SLOT_RE.test(code)) { return } + if (!HAS_SLOT_OR_CLIENT_RE.test(code)) { return } const template = code.match(TEMPLATE_RE) if (!template) { return } const startingIndex = template.index || 0 diff --git a/packages/nuxt/test/islandTransform.test.ts b/packages/nuxt/test/islandTransform.test.ts new file mode 100644 index 0000000000..75c80d3c60 --- /dev/null +++ b/packages/nuxt/test/islandTransform.test.ts @@ -0,0 +1,183 @@ +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: '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, + rootDir: '' +}, { framework: 'vite' }) as Plugin + +const pluginWebpack = islandsTransform.raw({ + getComponents, + rootDir: '' +}, { framework: 'webpack', webpack: { compiler: {} as any } }) + +const viteTransform = async (source: string, id: string) => { + const result = await (pluginVite.transform! as Function).call({ error: null, warn: null } as any, 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).call({ error: null, warn: null } as any, 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 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') + }) + }) +})