|
| 1 | +import { fileURLToPath } from 'node:url' |
| 2 | +import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'pathe' |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 6 | +import { buildNuxt, loadNuxt } from '@nuxt/kit' |
| 7 | + |
| 8 | +const nuxtDir = fileURLToPath(new URL('../../nuxt', import.meta.url)) |
| 9 | + |
| 10 | +describe('vite dev server allowedHosts', () => { |
| 11 | + let rootDir: string |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + rootDir = await realpath(await mkdtemp(join(tmpdir(), 'nuxt-allowed-hosts-'))) |
| 15 | + await mkdir(join(rootDir, 'app'), { recursive: true }) |
| 16 | + await mkdir(join(rootDir, 'node_modules'), { recursive: true }) |
| 17 | + await symlink(nuxtDir, join(rootDir, 'node_modules/nuxt'), 'dir') |
| 18 | + await writeFile(join(rootDir, 'app/app.vue'), '<template><div/></template>') |
| 19 | + await writeFile(join(rootDir, 'package.json'), '{}') |
| 20 | + }, 30000) |
| 21 | + |
| 22 | + afterAll(async () => { |
| 23 | + await rm(rootDir, { recursive: true, force: true }) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should list each allowed host once', async () => { |
| 27 | + const nuxt = await loadNuxt({ |
| 28 | + cwd: rootDir, |
| 29 | + dev: true, |
| 30 | + overrides: { |
| 31 | + devServer: { host: 'my-machine.lan' }, |
| 32 | + vite: { server: { allowedHosts: ['proxy.test'] } }, |
| 33 | + }, |
| 34 | + }) |
| 35 | + let allowedHosts: unknown |
| 36 | + nuxt.hook('vite:extend', (ctx) => { |
| 37 | + allowedHosts = ctx.config.server?.allowedHosts |
| 38 | + }) |
| 39 | + |
| 40 | + try { |
| 41 | + await buildNuxt(nuxt) |
| 42 | + expect(allowedHosts).toEqual(['proxy.test', 'my-machine.lan']) |
| 43 | + } finally { |
| 44 | + await nuxt.close() |
| 45 | + } |
| 46 | + }, 60000) |
| 47 | +}) |
0 commit comments