|
| 1 | +import{mkdir,mkdtemp,rm,writeFile}from'node:fs/promises' |
| 2 | +import{tmpdir}from'node:os' |
| 3 | + |
| 4 | +import{runCommand}from'citty' |
| 5 | +import{join}from'pathe' |
| 6 | +import{afterEach,beforeEach,describe,expect,it,vi}from'vitest' |
| 7 | + |
| 8 | +importpreviewfrom'../../../src/commands/preview' |
| 9 | + |
| 10 | +const{ loadKit, loadNuxt, x }=vi.hoisted(()=>({ |
| 11 | +loadKit: vi.fn(), |
| 12 | +loadNuxt: vi.fn(), |
| 13 | +x: vi.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('../../../src/utils/kit',()=>({ loadKit })) |
| 17 | +vi.mock('tinyexec',()=>({ x })) |
| 18 | + |
| 19 | +letcwd: string |
| 20 | + |
| 21 | +asyncfunctionwriteNitroJSON(outputDir: string,data: Record<string,unknown>={}){ |
| 22 | +awaitmkdir(outputDir,{recursive: true}) |
| 23 | +awaitwriteFile(join(outputDir,'nitro.json'),JSON.stringify({ |
| 24 | +preset: 'node-server', |
| 25 | +commands: {preview: 'node ./server/index.mjs'}, |
| 26 | + ...data, |
| 27 | +})) |
| 28 | +} |
| 29 | + |
| 30 | +describe('preview',()=>{ |
| 31 | +beforeEach(async()=>{ |
| 32 | +cwd=awaitmkdtemp(join(tmpdir(),'nuxt-preview-test-')) |
| 33 | +loadKit.mockResolvedValue({ loadNuxt }) |
| 34 | +loadNuxt.mockImplementation(async(options)=>{ |
| 35 | +constnuxt={ |
| 36 | +options: {srcDir: cwd}, |
| 37 | +hook: vi.fn((name,callback)=>{ |
| 38 | +if(name==='nitro:init'){ |
| 39 | +callback({options: {output: {dir: '.output'}}}) |
| 40 | +} |
| 41 | +}), |
| 42 | +close: vi.fn(), |
| 43 | +} |
| 44 | +options.overrides.modules[0](undefined,nuxt) |
| 45 | +returnnuxt |
| 46 | +}) |
| 47 | +x.mockResolvedValue({exitCode: 0}) |
| 48 | +}) |
| 49 | + |
| 50 | +afterEach(async()=>{ |
| 51 | +vi.unstubAllEnvs() |
| 52 | +vi.clearAllMocks() |
| 53 | +awaitrm(cwd,{recursive: true,force: true}) |
| 54 | +}) |
| 55 | + |
| 56 | +it('runs the build preview command with normalized whitespace and listen options',async()=>{ |
| 57 | +constoutputDir=join(cwd,'.output') |
| 58 | +awaitwriteNitroJSON(outputDir) |
| 59 | + |
| 60 | +awaitrunCommand(preview,{ |
| 61 | +rawArgs: [cwd,'--port=4321','--host=127.0.0.1'], |
| 62 | +}) |
| 63 | + |
| 64 | +expect(x).toHaveBeenCalledWith('node',['./server/index.mjs'],{ |
| 65 | +throwOnError: true, |
| 66 | +nodeOptions: expect.objectContaining({ |
| 67 | +cwd: outputDir, |
| 68 | +env: expect.objectContaining({ |
| 69 | +NUXT_PORT: '4321', |
| 70 | +NITRO_PORT: '4321', |
| 71 | +NUXT_HOST: '127.0.0.1', |
| 72 | +NITRO_HOST: '127.0.0.1', |
| 73 | +}), |
| 74 | +}), |
| 75 | +}) |
| 76 | +}) |
| 77 | + |
| 78 | +it('uses the configured output directory',async()=>{ |
| 79 | +constoutputDir=join(cwd,'dist','server-output') |
| 80 | +awaitwriteNitroJSON(outputDir) |
| 81 | +loadNuxt.mockImplementation(async(options)=>{ |
| 82 | +constnuxt={ |
| 83 | +options: {srcDir: join(cwd,'src')}, |
| 84 | +hook: vi.fn((name,callback)=>{ |
| 85 | +if(name==='nitro:init'){ |
| 86 | +callback({options: {output: {dir: '../dist/server-output'}}}) |
| 87 | +} |
| 88 | +}), |
| 89 | +close: vi.fn(), |
| 90 | +} |
| 91 | +options.overrides.modules[0](undefined,nuxt) |
| 92 | +returnnuxt |
| 93 | +}) |
| 94 | + |
| 95 | +awaitrunCommand(preview,{rawArgs: [cwd]}) |
| 96 | + |
| 97 | +expect(x).toHaveBeenCalledWith('node',['./server/index.mjs'],expect.objectContaining({ |
| 98 | +nodeOptions: expect.objectContaining({cwd: outputDir}), |
| 99 | +})) |
| 100 | +}) |
| 101 | + |
| 102 | +it('falls back to the conventional output when Nuxt cannot load',async()=>{ |
| 103 | +loadKit.mockRejectedValue(newError('Nuxt is unavailable')) |
| 104 | +constoutputDir=join(cwd,'.output') |
| 105 | +awaitwriteNitroJSON(outputDir) |
| 106 | + |
| 107 | +awaitrunCommand(preview,{rawArgs: [cwd]}) |
| 108 | + |
| 109 | +expect(x).toHaveBeenCalledWith('node',['./server/index.mjs'],expect.objectContaining({ |
| 110 | +nodeOptions: expect.objectContaining({cwd: outputDir}), |
| 111 | +})) |
| 112 | +}) |
| 113 | + |
| 114 | +it('uses host and port environment variables',async()=>{ |
| 115 | +vi.stubEnv('NUXT_PORT','4100') |
| 116 | +vi.stubEnv('NUXT_HOST','localhost') |
| 117 | +awaitwriteNitroJSON(join(cwd,'.output')) |
| 118 | + |
| 119 | +awaitrunCommand(preview,{rawArgs: [cwd]}) |
| 120 | + |
| 121 | +expect(x).toHaveBeenCalledWith(expect.any(String),expect.any(Array),expect.objectContaining({ |
| 122 | +nodeOptions: expect.objectContaining({ |
| 123 | +env: expect.objectContaining({ |
| 124 | +NUXT_PORT: '4100', |
| 125 | +NITRO_PORT: '4100', |
| 126 | +NUXT_HOST: 'localhost', |
| 127 | +NITRO_HOST: 'localhost', |
| 128 | +}), |
| 129 | +}), |
| 130 | +})) |
| 131 | +}) |
| 132 | +}) |
0 commit comments