diff --git a/packages/test-utils/build.config.ts b/packages/test-utils/build.config.ts index 4b6640762b1..e0f61579a17 100644 --- a/packages/test-utils/build.config.ts +++ b/packages/test-utils/build.config.ts @@ -4,6 +4,7 @@ export default defineBuildConfig({ declaration: true, entries: [ 'src/index', + 'src/experimental', { input: 'src/runtime/', outDir: 'dist/runtime', format: 'esm' } ], externals: [ diff --git a/packages/test-utils/experimental.d.ts b/packages/test-utils/experimental.d.ts new file mode 100644 index 00000000000..c9d24adffad --- /dev/null +++ b/packages/test-utils/experimental.d.ts @@ -0,0 +1 @@ +export * from './dist/experimental' diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 91b3f78c200..d0ce8c87e4b 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -9,6 +9,10 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.mjs" + }, + "./experimental": { + "types": "./dist/experimental.d.ts", + "import": "./dist/experimental.mjs" } }, "files": [ diff --git a/packages/test-utils/src/experimental.ts b/packages/test-utils/src/experimental.ts new file mode 100644 index 00000000000..3998fbad323 --- /dev/null +++ b/packages/test-utils/src/experimental.ts @@ -0,0 +1,22 @@ +import { fetch as _fetch, $fetch as _$fetch } from 'ofetch' +import * as _kit from '@nuxt/kit' +import { resolve } from 'pathe' +import { stringifyQuery } from 'ufo' +import { useTestContext } from './context' + +/** + * This is a function to render a component directly with the Nuxt server. + */ +export function $fetchComponent (filepath: string, props?: Record) { + return $fetch(componentTestUrl(filepath, props)) +} + +export function componentTestUrl (filepath: string, props?: Record) { + const ctx = useTestContext() + filepath = resolve(ctx.options.rootDir, filepath) + const path = stringifyQuery({ + path: filepath, + props: JSON.stringify(props) + }) + return `/__nuxt_component_test__/?${path}` +} diff --git a/packages/test-utils/src/server.ts b/packages/test-utils/src/server.ts index 06e4fc812eb..77b580e2ae3 100644 --- a/packages/test-utils/src/server.ts +++ b/packages/test-utils/src/server.ts @@ -4,7 +4,6 @@ import type { FetchOptions } from 'ofetch' import { fetch as _fetch, $fetch as _$fetch } from 'ofetch' import * as _kit from '@nuxt/kit' import { resolve } from 'pathe' -import { stringifyQuery } from 'ufo' import { useTestContext } from './context' // @ts-ignore type cast @@ -71,20 +70,6 @@ export function $fetch (path: string, options?: FetchOptions) { return _$fetch(url(path), options) } -export function $fetchComponent (filepath: string, props?: Record) { - return $fetch(componentTestUrl(filepath, props)) -} - -export function componentTestUrl (filepath: string, props?: Record) { - const ctx = useTestContext() - filepath = resolve(ctx.options.rootDir, filepath) - const path = stringifyQuery({ - path: filepath, - props: JSON.stringify(props) - }) - return `/__nuxt_component_test__/?${path}` -} - export function url (path: string) { const ctx = useTestContext() if (!ctx.url) { diff --git a/test/basic.test.ts b/test/basic.test.ts index 3161afec11a..d5391d270cb 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -2,11 +2,12 @@ import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' import { joinURL, withQuery } from 'ufo' import { isCI, isWindows } from 'std-env' -import { join, normalize } from 'pathe' -import { setup, fetch, $fetch, $fetchComponent, startServer, isDev, createPage, url } from '@nuxt/test-utils' +import { normalize } from 'pathe' +import { setup, fetch, $fetch, startServer, isDev, createPage, url } from '@nuxt/test-utils' import type { NuxtIslandResponse } from '../packages/nuxt/src/core/runtime/nitro/renderer' import { expectNoClientErrors, expectWithPolling, renderPage, withLogs } from './utils' +import { $fetchComponent } from '@nuxt/test-utils/experimental' const isWebpack = process.env.TEST_BUILDER === 'webpack' diff --git a/vitest.config.ts b/vitest.config.ts index 6a054ad90c5..048dfb643bd 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -7,6 +7,7 @@ export default defineConfig({ resolve: { alias: { '#app': resolve('./packages/nuxt/dist/app/index'), + '@nuxt/test-utils/experimental': resolve('./packages/test-utils/src/experimental.ts'), '@nuxt/test-utils': resolve('./packages/test-utils/src/index.ts') } },