Skip to content

Commit 0b45675

Browse files
committed
test: assert nuxt info reports config keys without their values
1 parent 3a5aa2c commit 0b45675

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import{mkdtemp,rm,writeFile}from'node:fs/promises'
2+
import{tmpdir}from'node:os'
3+
importprocessfrom'node:process'
4+
5+
import{runCommand}from'citty'
6+
import{join}from'pathe'
7+
import{afterEach,beforeEach,describe,expect,it,vi}from'vitest'
8+
9+
importinfofrom'../../../src/commands/info'
10+
import{render,screen}from'../../utils/terminal'
11+
12+
vi.mock('tinyclip',()=>({writeText: ()=>Promise.reject(newError('no clipboard'))}))
13+
14+
letcwd: string
15+
16+
beforeEach(async()=>{
17+
cwd=awaitmkdtemp(join(tmpdir(),'nuxt-info-'))
18+
})
19+
20+
afterEach(async()=>{
21+
awaitrm(cwd,{recursive: true,force: true})
22+
})
23+
24+
asyncfunctionrunInfo(): Promise<string>{
25+
constterminal=awaitrender(()=>runCommand(info,{rawArgs: [`--cwd=${cwd}`]}))
26+
returnscreen(terminal)
27+
}
28+
29+
asyncfunctionrunInfoJSON(): Promise<any>{
30+
letstdout=''
31+
vi.spyOn(process.stdout,'write').mockImplementation((chunk: any)=>{
32+
stdout+=String(chunk)
33+
returntrue
34+
})
35+
awaitrunCommand(info,{rawArgs: [`--cwd=${cwd}`,'--json']})
36+
returnJSON.parse(stdout)
37+
}
38+
39+
describe('info command',()=>{
40+
it('should report the config keys without their values',async()=>{
41+
awaitwriteFile(join(cwd,'package.json'),JSON.stringify({name: 'app',private: true}))
42+
awaitwriteFile(join(cwd,'nuxt.config.mjs'),`export default {
43+
runtimeConfig: { apiSecret: 'super-secret-token' },
44+
devServer: { host: '10.0.0.1' },
45+
}`)
46+
47+
constoutput=awaitrunInfo()
48+
49+
expect(output).toContain('runtimeConfig')
50+
expect(output).not.toContain('super-secret-token')
51+
expect(output).not.toContain('10.0.0.1')
52+
})
53+
54+
it('should not print environment variables',async()=>{
55+
vi.stubEnv('NUXT_SECRET_TOKEN','env-secret-value')
56+
awaitwriteFile(join(cwd,'package.json'),JSON.stringify({name: 'app',private: true}))
57+
58+
constoutput=awaitrunInfo()
59+
60+
expect(output).not.toContain('env-secret-value')
61+
vi.unstubAllEnvs()
62+
})
63+
64+
it('should list the modules a config declares',async()=>{
65+
awaitwriteFile(join(cwd,'package.json'),JSON.stringify({name: 'app',private: true}))
66+
awaitwriteFile(join(cwd,'nuxt.config.mjs'),`export default { modules: ['@nuxt/image'] }`)
67+
68+
constoutput=awaitrunInfo()
69+
70+
expect(output).toContain('@nuxt/image')
71+
})
72+
73+
it('should keep a comma in a config key or module path as one `--json` entry',async()=>{
74+
awaitwriteFile(join(cwd,'package.json'),JSON.stringify({name: 'app',private: true}))
75+
awaitwriteFile(join(cwd,'nuxt.config.mjs'),`export default {
76+
modules: ['./modules/a, b.ts'],
77+
'weird, key': true,
78+
}`)
79+
80+
constpayload=awaitrunInfoJSON()
81+
82+
expect(payload.config).toContain('weird, key')
83+
expect(payload.modules).toEqual(['./modules/a, b.ts'])
84+
})
85+
86+
it('should still report on a project with no config',async()=>{
87+
awaitwriteFile(join(cwd,'package.json'),JSON.stringify({name: 'app',private: true}))
88+
89+
constoutput=awaitrunInfo()
90+
91+
expect(output).toContain('Nuxt version')
92+
})
93+
})

0 commit comments

Comments
 (0)