Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
refactor: move to /experimental subpath
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 6, 2023
1 parent 22d9777 commit e14ddd2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/test-utils/build.config.ts
Expand Up @@ -4,6 +4,7 @@ export default defineBuildConfig({
declaration: true,
entries: [
'src/index',
'src/experimental',
{ input: 'src/runtime/', outDir: 'dist/runtime', format: 'esm' }
],
externals: [
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/experimental.d.ts
@@ -0,0 +1 @@
export * from './dist/experimental'
4 changes: 4 additions & 0 deletions packages/test-utils/package.json
Expand Up @@ -9,6 +9,10 @@
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
},
"./experimental": {
"types": "./dist/experimental.d.ts",
"import": "./dist/experimental.mjs"
}
},
"files": [
Expand Down
22 changes: 22 additions & 0 deletions 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<string, any>) {
return $fetch(componentTestUrl(filepath, props))
}

export function componentTestUrl (filepath: string, props?: Record<string, any>) {
const ctx = useTestContext()
filepath = resolve(ctx.options.rootDir, filepath)
const path = stringifyQuery({
path: filepath,
props: JSON.stringify(props)
})
return `/__nuxt_component_test__/?${path}`
}
15 changes: 0 additions & 15 deletions packages/test-utils/src/server.ts
Expand Up @@ -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
Expand Down Expand Up @@ -71,20 +70,6 @@ export function $fetch (path: string, options?: FetchOptions) {
return _$fetch(url(path), options)
}

export function $fetchComponent (filepath: string, props?: Record<string, any>) {
return $fetch(componentTestUrl(filepath, props))
}

export function componentTestUrl (filepath: string, props?: Record<string, any>) {
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) {
Expand Down
5 changes: 3 additions & 2 deletions test/basic.test.ts
Expand Up @@ -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'

Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Expand Up @@ -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')
}
},
Expand Down

0 comments on commit e14ddd2

Please sign in to comment.