2021-08-09 17:30:25 +00:00
|
|
|
import { pathToFileURL } from 'url'
|
2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-07-15 09:38:06 +00:00
|
|
|
import destr from 'destr'
|
2022-01-13 17:54:33 +00:00
|
|
|
import { listen, Listener } from 'listhen'
|
2021-09-05 21:54:12 +00:00
|
|
|
import { $fetch } from 'ohmyfetch'
|
2021-11-18 18:12:19 +00:00
|
|
|
import { execa } from 'execa'
|
2022-01-13 17:54:33 +00:00
|
|
|
import { expect, it, beforeAll, afterAll } from 'vitest'
|
|
|
|
import { fixtureDir, resolveWorkspace } from '../utils'
|
2021-07-15 09:38:06 +00:00
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
const isBridge = Boolean(process.env.TEST_BRIDGE)
|
2021-07-15 09:38:06 +00:00
|
|
|
|
2022-01-13 17:54:33 +00:00
|
|
|
interface Context {
|
|
|
|
rootDir: string
|
|
|
|
outDir: string
|
|
|
|
fetch: (url:string) => Promise<any>
|
|
|
|
server?: Listener
|
|
|
|
}
|
|
|
|
|
2021-11-10 12:06:22 +00:00
|
|
|
export function importModule (path) {
|
2021-08-09 17:30:25 +00:00
|
|
|
return import(pathToFileURL(path).href)
|
2021-07-15 09:38:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 12:06:22 +00:00
|
|
|
export function setupTest (preset) {
|
2021-09-05 21:21:33 +00:00
|
|
|
const fixture = isBridge ? 'bridge' : 'basic'
|
2021-07-15 09:38:06 +00:00
|
|
|
const rootDir = fixtureDir(fixture)
|
|
|
|
const buildDir = resolve(rootDir, '.nuxt-' + preset)
|
|
|
|
|
2022-01-13 17:54:33 +00:00
|
|
|
const ctx: Context = {
|
2021-07-15 09:38:06 +00:00
|
|
|
rootDir,
|
|
|
|
outDir: resolve(buildDir, 'output'),
|
2022-01-13 17:54:33 +00:00
|
|
|
fetch: url => $fetch(url, { baseURL: ctx.server!.url })
|
2021-07-15 09:38:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:54:33 +00:00
|
|
|
beforeAll(async () => {
|
2021-09-05 21:21:33 +00:00
|
|
|
const nuxtCLI = isBridge
|
2021-11-11 16:46:10 +00:00
|
|
|
? resolve(ctx.rootDir, 'node_modules/nuxt-edge/bin/nuxt.js')
|
2021-10-02 16:01:17 +00:00
|
|
|
: resolveWorkspace('packages/nuxi/bin/nuxi.mjs')
|
2021-07-15 09:38:06 +00:00
|
|
|
|
|
|
|
await execa('node', [nuxtCLI, 'build', ctx.rootDir], {
|
|
|
|
env: {
|
|
|
|
NITRO_PRESET: preset,
|
|
|
|
NITRO_BUILD_DIR: buildDir,
|
|
|
|
NITRO_OUTPUT_DIR: ctx.outDir,
|
|
|
|
NODE_ENV: 'production'
|
|
|
|
}
|
|
|
|
})
|
2022-01-13 17:54:33 +00:00
|
|
|
}, (isBridge ? 120 : 60) * 1000)
|
2021-07-15 09:38:06 +00:00
|
|
|
|
2022-01-13 17:54:33 +00:00
|
|
|
afterAll(async () => {
|
2021-07-15 09:38:06 +00:00
|
|
|
if (ctx.server) {
|
|
|
|
await ctx.server.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-11-10 12:06:22 +00:00
|
|
|
export async function startServer (ctx, handle) {
|
2021-07-15 09:38:06 +00:00
|
|
|
ctx.server = await listen(handle)
|
|
|
|
}
|
|
|
|
|
2021-11-10 12:06:22 +00:00
|
|
|
export function testNitroBehavior (_ctx, getHandler) {
|
2021-07-15 09:38:06 +00:00
|
|
|
let handler
|
|
|
|
|
|
|
|
it('setup handler', async () => {
|
|
|
|
handler = await getHandler()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('SSR Works', async () => {
|
|
|
|
const { data } = await handler({ url: '/' })
|
|
|
|
expect(data).to.have.string('Hello Vue')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('API Works', async () => {
|
2021-10-22 17:04:02 +00:00
|
|
|
const { data: helloData } = await handler({ url: '/api/hello' })
|
|
|
|
const { data: heyData } = await handler({ url: '/api/hey' })
|
|
|
|
expect(destr(helloData)).to.have.string('Hello API')
|
|
|
|
expect(destr(heyData)).to.have.string('Hey API')
|
2021-07-15 09:38:06 +00:00
|
|
|
})
|
|
|
|
}
|