|
| 1 | +import{mkdir,mkdtemp,readFile,realpath,symlink,writeFile}from'node:fs/promises' |
| 2 | +import{tmpdir}from'node:os' |
| 3 | +import{fileURLToPath}from'node:url' |
| 4 | + |
| 5 | +import{dirname,join}from'pathe' |
| 6 | +import{afterAll,afterEach,describe,expect,it,vi}from'vitest' |
| 7 | + |
| 8 | +import{addNuxtConfigEntries,readNuxtConfig}from'../../../src/utils/config' |
| 9 | + |
| 10 | +constrolldownPath=dirname(fileURLToPath(import.meta.resolve('rolldown/package.json'))) |
| 11 | + |
| 12 | +constdirectories: string[]=[] |
| 13 | + |
| 14 | +afterEach(()=>{ |
| 15 | +vi.unstubAllEnvs() |
| 16 | +}) |
| 17 | + |
| 18 | +afterAll(async()=>{ |
| 19 | +const{ rm }=awaitimport('node:fs/promises') |
| 20 | +awaitPromise.all(directories.splice(0).map(directory=>rm(directory,{recursive: true,force: true}))) |
| 21 | +}) |
| 22 | + |
| 23 | +asyncfunctioncreateProject(source: string,{ parser }: {parser: boolean}): Promise<string>{ |
| 24 | +constcwd=awaitrealpath(awaitmkdtemp(join(tmpdir(),'nuxi-config-dynamic-'))) |
| 25 | +directories.push(cwd) |
| 26 | +awaitwriteFile(join(cwd,'nuxt.config.ts'),source,'utf8') |
| 27 | +if(parser){ |
| 28 | +awaitmkdir(join(cwd,'node_modules'),{recursive: true}) |
| 29 | +awaitsymlink(rolldownPath,join(cwd,'node_modules/rolldown'),'dir') |
| 30 | +} |
| 31 | +else{ |
| 32 | +vi.stubEnv('NUXT_CLI_PARSER','scanner') |
| 33 | +} |
| 34 | +returncwd |
| 35 | +} |
| 36 | + |
| 37 | +describe.each([ |
| 38 | +['scanner',{parser: false}], |
| 39 | +['parser',{parser: true}], |
| 40 | +])('%s',(name,options)=>{ |
| 41 | +constrefusal=name==='parser' ? 'Could not find a config object' : 'Default export is missing' |
| 42 | +asyncfunctionadd(source: string): Promise<{error?: string,after: string}>{ |
| 43 | +constcwd=awaitcreateProject(source,options) |
| 44 | +constconfig=awaitreadNuxtConfig(cwd) |
| 45 | +leterror: string|undefined |
| 46 | +try{ |
| 47 | +awaitaddNuxtConfigEntries(config!,{modules: ['@nuxt/image']}) |
| 48 | +} |
| 49 | +catch(err){ |
| 50 | +error=(errasError).message |
| 51 | +} |
| 52 | +return{ error,after: awaitreadFile(join(cwd,'nuxt.config.ts'),'utf8')} |
| 53 | +} |
| 54 | + |
| 55 | +it('should refuse to add a key to a config that spreads another object',async()=>{ |
| 56 | +constsource='export default defineNuxtConfig({ ...base })\n' |
| 57 | + |
| 58 | +const{ error, after }=awaitadd(source) |
| 59 | + |
| 60 | +expect(error).toContain('silently overridden') |
| 61 | +expect(after).toBe(source) |
| 62 | +}) |
| 63 | + |
| 64 | +it('should never end up with two `modules` keys next to a spread',async()=>{ |
| 65 | +const{ error, after }=awaitadd('export default defineNuxtConfig({ ...base, modules: [\'a\'] })\n') |
| 66 | + |
| 67 | +expect(after.match(/modules:/g)).toHaveLength(1) |
| 68 | +if(error){ |
| 69 | +expect(error).toContain('silently overridden') |
| 70 | +} |
| 71 | +else{ |
| 72 | +expect(after).toContain('modules: [\'a\', \'@nuxt/image\']') |
| 73 | +} |
| 74 | +}) |
| 75 | + |
| 76 | +it('should refuse to add a key next to a computed one',async()=>{ |
| 77 | +constsource='const key = \'modules\'\nexport default defineNuxtConfig({ [key]: [\'a\'] })\n' |
| 78 | + |
| 79 | +const{ error, after }=awaitadd(source) |
| 80 | + |
| 81 | +expect(error).toContain('silently overridden') |
| 82 | +expect(after).toBe(source) |
| 83 | +}) |
| 84 | + |
| 85 | +it('should refuse a config whose default export is not an object',async()=>{ |
| 86 | +constcwd=awaitcreateProject('export default defineNuxtConfig(base)\n',options) |
| 87 | + |
| 88 | +awaitexpect(readNuxtConfig(cwd)).rejects.toThrow(refusal) |
| 89 | +}) |
| 90 | + |
| 91 | +it('should refuse a config chosen by a conditional',async()=>{ |
| 92 | +constcwd=awaitcreateProject('export default defineNuxtConfig(x ? { modules: [] } : { modules: [\'a\'] })\n',options) |
| 93 | + |
| 94 | +awaitexpect(readNuxtConfig(cwd)).rejects.toThrow(refusal) |
| 95 | +}) |
| 96 | + |
| 97 | +it('should refuse a config returned from a function',async()=>{ |
| 98 | +constcwd=awaitcreateProject('export default defineNuxtConfig(() => ({ modules: [] }))\n',options) |
| 99 | + |
| 100 | +awaitexpect(readNuxtConfig(cwd)).rejects.toThrow(refusal) |
| 101 | +}) |
| 102 | + |
| 103 | +it('should add a key to an ordinary config',async()=>{ |
| 104 | +const{ error, after }=awaitadd('export default defineNuxtConfig({ ssr: false })\n') |
| 105 | + |
| 106 | +expect(error).toBeUndefined() |
| 107 | +expect(after).toContain('@nuxt/image') |
| 108 | +}) |
| 109 | +}) |
0 commit comments