2021-07-15 09:38:06 +00:00
|
|
|
import { resolve } from 'path'
|
2021-08-09 17:30:25 +00:00
|
|
|
import { pathToFileURL } from 'url'
|
2021-07-15 09:38:06 +00:00
|
|
|
import destr from 'destr'
|
|
|
|
import { listen } from 'listhen'
|
|
|
|
import { $fetch } from 'ohmyfetch/node'
|
|
|
|
import execa from 'execa'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import { fixtureDir, resolveWorkspace } from '../utils.mjs'
|
|
|
|
|
|
|
|
const isCompat = Boolean(process.env.TEST_COMPAT)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export function setupTest (preset) {
|
|
|
|
const fixture = isCompat ? 'compat' : 'basic'
|
|
|
|
const rootDir = fixtureDir(fixture)
|
|
|
|
const buildDir = resolve(rootDir, '.nuxt-' + preset)
|
|
|
|
|
|
|
|
const ctx = {
|
|
|
|
rootDir,
|
|
|
|
outDir: resolve(buildDir, 'output'),
|
|
|
|
fetch: url => $fetch(url, { baseURL: ctx.server.url })
|
|
|
|
}
|
|
|
|
|
|
|
|
it('nitro build', async () => {
|
|
|
|
const nuxtCLI = isCompat
|
|
|
|
? resolve(ctx.rootDir, 'node_modules/nuxt/bin/nuxt.js')
|
2021-08-10 17:37:03 +00:00
|
|
|
: resolveWorkspace('packages/nuxi/bin/nuxi.js')
|
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'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}).timeout(60000)
|
|
|
|
|
|
|
|
after('Cleanup', async () => {
|
|
|
|
if (ctx.server) {
|
|
|
|
await ctx.server.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function startServer (ctx, handle) {
|
|
|
|
ctx.server = await listen(handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function testNitroBehavior (ctx, getHandler) {
|
|
|
|
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 () => {
|
|
|
|
const { data } = await handler({ url: '/api/hello' })
|
|
|
|
expect(destr(data)).to.have.string('Hello API')
|
|
|
|
})
|
|
|
|
}
|