chore: progress

This commit is contained in:
Harlan Wilton 2023-03-06 13:02:11 +11:00
parent 84030919a5
commit dd366aa494
5 changed files with 42 additions and 38 deletions

View File

@ -46,14 +46,14 @@ export default defineNuxtCommand({
const { loadNuxt, loadNuxtConfig, buildNuxt } = await loadKit(rootDir)
console.log('overrides', ...(process.env.NUXT_CONFIG_OVERRIDES ? JSON.parse(process.env.NUXT_CONFIG_OVERRIDES) : {}))
const config = await loadNuxtConfig({
cwd: rootDir,
overrides: {
const overrides = {
dev: true,
// used for testing
...(process.env.NUXT_CONFIG_OVERRIDES ? JSON.parse(process.env.NUXT_CONFIG_OVERRIDES) : {})
}
const config = await loadNuxtConfig({
cwd: rootDir,
overrides
})
const listener = await listen(serverHandler, {
@ -93,7 +93,7 @@ export default defineNuxtCommand({
await distWatcher.close()
}
currentNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
currentNuxt = await loadNuxt({ rootDir, overrides, dev: true, ready: false })
currentNuxt.hooks.hookOnce('restart', async (options) => {
if (options?.hard && process.send) {

View File

@ -16,7 +16,7 @@ const isNuxtApp = (dir: string) => {
)
}
const resolveRootDir = () => {
export const resolveRootDir = () => {
const { options } = useTestContext()
const dirs = [
@ -37,9 +37,6 @@ const resolveRootDir = () => {
export async function loadFixture () {
const ctx = useTestContext()
ctx.options.rootDir = resolveRootDir()
if (!ctx.options.dev) {
const randomId = Math.random().toString(36).slice(2, 8)
const buildDir = resolve(ctx.options.rootDir, '.nuxt', randomId)
Object.assign(ctx.options.nuxtConfig, {
@ -50,7 +47,6 @@ export async function loadFixture () {
}
}
})
}
ctx.nuxt = await kit.loadNuxt({
cwd: ctx.options.rootDir,

View File

@ -15,7 +15,7 @@ export async function startDevServer () {
const port = await getRandomPort()
const nuxiCLI = await kit.resolvePath('nuxi/cli')
ctx.serverProcess = execa(nuxiCLI, ['dev'], {
cwd: ctx.nuxt!.options.rootDir,
cwd: ctx.options.rootDir,
env: {
...process.env,
PORT: String(port),
@ -24,6 +24,7 @@ export async function startDevServer () {
NUXT_CONFIG_OVERRIDES: JSON.stringify(ctx.options.nuxtConfig)
}
})
console.log('boot dev server', ctx.options.rootDir, JSON.stringify(ctx.options.nuxtConfig))
// wait until the server process displays the listening path
ctx.url = await new Promise<string>(resolve => {
// @ts-expect-error untyoed

View File

@ -1,5 +1,5 @@
import { createTestContext, setTestContext } from '../context'
import { loadFixture, buildFixture } from '../nuxt'
import {createTestContext, setTestContext, useTestContext} from '../context'
import {loadFixture, buildFixture, resolveRootDir} from '../nuxt'
import {startDevServer, startServer, stopServer} from '../server'
import { createBrowser } from '../browser'
import type { TestHooks, TestOptions } from '../types'
@ -37,13 +37,16 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
}
const setup = async () => {
if (ctx.options.fixture) {
await loadFixture()
}
const ctx = useTestContext()
ctx.options.rootDir = resolveRootDir()
if (ctx.options.dev) {
await startDevServer()
} else {
if (ctx.options.fixture) {
await loadFixture()
}
if (ctx.options.build) {
await buildFixture()
}

View File

@ -4,25 +4,29 @@ import { setup, $fetch } from '@nuxt/test-utils'
import { describe, it, expect } from 'vitest'
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
rootDir: fileURLToPath(new URL('./fixtures/minimal', import.meta.url)),
dev: true,
nuxtConfig: {
app: {
baseURL: '/test'
},
runtimeConfig: {
public: {
foo: 'bar',
}
}
}
})
describe('dev tests', () => {
it('should just work', async () => {
expect(await $fetch('/')).toMatchInlineSnapshot(`
"<!DOCTYPE html>
<html >
<head><meta charset=\\"utf-8\\">
<meta name=\\"viewport\\" content=\\"width=1024, initial-scale=1\\">
<title>- Fixture</title><link rel=\\"preload\\" as=\\"style\\" href=\\"/_nuxt/assets/global.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"/_nuxt/virtual.css\\"><link rel=\\"preload\\" as=\\"style\\" href=\\"/_nuxt/assets/plugin.css\\"><link rel=\\"modulepreload\\" as=\\"script\\" crossorigin href=\\"/_nuxt/home/harlan/forks/nuxt3/packages/nuxt/src/app/entry.ts\\"><link rel=\\"stylesheet\\" href=\\"/_nuxt/assets/global.css\\"><link rel=\\"stylesheet\\" href=\\"/_nuxt/virtual.css\\"><link rel=\\"stylesheet\\" href=\\"/_nuxt/assets/plugin.css\\"></head>
<body ><div id=\\"__nuxt\\"><div><div>Extended layout from foo</div><div><h1>[...slug].vue</h1><div>catchall at &gt; Network: http:</div><div>Middleware ran: true</div></div></div></div><script>window.__NUXT__=(function(a){return {data:{hey:{foo:\\"bar\\",baz:\\"qux\\"}},state:{},_errors:{},serverRendered:true,config:{public:{ids:[1,2,3],needsFallback:a,testConfig:123},app:{baseURL:\\"\\\\u002F\\",buildAssetsDir:\\"\\\\u002F_nuxt\\\\u002F\\",cdnURL:a}},prerenderedAt:1678028804677}}(\\"\\"))</script><script type=\\"module\\" src=\\"/_nuxt/@vite/client\\" crossorigin></script><script type=\\"module\\" src=\\"/_nuxt/home/harlan/forks/nuxt3/packages/nuxt/src/app/entry.ts\\" crossorigin></script></body>
</html>"
`)
it('config is overriden', async () => {
const html = await $fetch('/')
const expr = /window.__NUXT__=({.*})<\/script>/.exec(html)![1]
expect(expr).toMatchInlineSnapshot('"{data:{},state:{},_errors:{},serverRendered:true,config:{public:{foo:\\"bar\\"},app:{baseURL:\\"\\\\u002Ftest\\",buildAssetsDir:\\"\\\\u002F_nuxt\\\\u002F\\",cdnURL:\\"\\"}}}"')
// expr is a javascript object string, but not valid JSON, we need to turn it a real object
const obj = JSON.parse(expr.replace(/(\w+):/g, '"$1":'))
expect(obj.config.public.foo).toBe('bar')
expect(obj.config.app.baseURL).toBe('/test')
})
})