refactor(cli): return object instead of array in getMcFlyConfig
This commit is contained in:
parent
708ead451c
commit
fee04025bb
8 changed files with 220 additions and 6 deletions
45
packages/core/src/cli/commands/build.test.ts
Normal file
45
packages/core/src/cli/commands/build.test.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import type { ParsedArgs } from 'citty'
|
||||
import consola from 'consola'
|
||||
import { expect, it, vi } from 'vitest'
|
||||
import { exportedForTest } from './build.js'
|
||||
|
||||
const build = exportedForTest.build
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
return {
|
||||
build: vi.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('nitropack', () => {
|
||||
return {
|
||||
build: mocks.build,
|
||||
}
|
||||
})
|
||||
|
||||
it('start build with message', () => {
|
||||
const message = 'Building project...'
|
||||
const spy = vi.spyOn(consola, 'start')
|
||||
|
||||
build({ dir: 'fakeDir', _: [] } as ParsedArgs)
|
||||
|
||||
expect(spy).toHaveBeenCalledWith(message)
|
||||
})
|
||||
|
||||
// test('execute nitropack build', () => {
|
||||
// mocks.build.mockImplementation(() => {})
|
||||
// testFn({ dir: '.' })
|
||||
|
||||
// expect(mocks.build).toHaveBeenCalled()
|
||||
// })
|
||||
|
||||
// test('catch error', () => {
|
||||
// const spy = vi.spyOn(consola, 'error')
|
||||
// mocks.build.mockImplementationOnce(() => {
|
||||
// throw new Error('hey')
|
||||
// })
|
||||
|
||||
// testFn()
|
||||
|
||||
// expect(spy).toHaveBeenCalledWith(new Error('hey'))
|
||||
// })
|
|
@ -20,7 +20,7 @@ async function _build(args: ParsedArgs) {
|
|||
const dir: string = args.dir?.toString() || args._dir?.toString() || '.'
|
||||
const rootDir = resolve(dir)
|
||||
|
||||
const [mcflyConfig, appConfigFile] = await getMcFlyConfig()
|
||||
const { mcflyConfig, configFile } = await getMcFlyConfig()
|
||||
const nitroConfig = await getNitroConfig(mcflyConfig)
|
||||
|
||||
const nitro = await createNitro({
|
||||
|
@ -41,7 +41,7 @@ async function _build(args: ParsedArgs) {
|
|||
handler: resolve(__dirname, '../../route-middleware.js'),
|
||||
})
|
||||
|
||||
nitro.options.runtimeConfig.appConfigFile = appConfigFile
|
||||
nitro.options.runtimeConfig.appConfigFile = configFile
|
||||
|
||||
await prepare(nitro)
|
||||
await copyPublicAssets(nitro)
|
||||
|
|
15
packages/core/src/cli/commands/generate.test.ts
Normal file
15
packages/core/src/cli/commands/generate.test.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { expect, it, vi } from 'vitest'
|
||||
import { exportedForTest } from './generate.js'
|
||||
import consola from 'consola'
|
||||
|
||||
const generate = exportedForTest.generate
|
||||
|
||||
it('show box message in-progress', () => {
|
||||
const spy = vi.spyOn(consola, 'box')
|
||||
|
||||
generate()
|
||||
const arg = spy.mock.calls[0][0]
|
||||
|
||||
expect(spy).toHaveBeenCalled()
|
||||
expect(arg).toContain('In-progress')
|
||||
})
|
80
packages/core/src/cli/commands/new.test.ts
Normal file
80
packages/core/src/cli/commands/new.test.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import { execSync } from 'node:child_process'
|
||||
import { consola } from 'consola'
|
||||
import { expect, vi } from 'vitest'
|
||||
import { exportedForTest } from './new.js'
|
||||
import { it } from 'node:test'
|
||||
|
||||
const createNew = exportedForTest.createNew
|
||||
const baseCommand = `npm create mcfly@latest`
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
return {
|
||||
execSync: vi.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('node:child_process', () => {
|
||||
return {
|
||||
execSync: mocks.execSync,
|
||||
}
|
||||
})
|
||||
|
||||
it('execute create mcfly', () => {
|
||||
const param = { stdio: 'inherit' }
|
||||
|
||||
createNew({ _: [] })
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(baseCommand, param)
|
||||
})
|
||||
|
||||
it('execute create mcfly with no dir', () => {
|
||||
const dir = 'fake-dir'
|
||||
const command = `${baseCommand} ${dir}`
|
||||
const param = { stdio: 'inherit' }
|
||||
|
||||
createNew({ _: [] })
|
||||
|
||||
expect(execSync).not.toHaveBeenCalledWith(command, param)
|
||||
})
|
||||
|
||||
it('execute create mcfly with dir', () => {
|
||||
const dir = 'fake-dir'
|
||||
const command = `${baseCommand} ${dir}`
|
||||
const param = { stdio: 'inherit' }
|
||||
|
||||
createNew({ dir, _: [] })
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command, param)
|
||||
})
|
||||
|
||||
it('execute create mcfly with _dir', () => {
|
||||
const dir = 'fake-dir'
|
||||
const command = `${baseCommand} ${dir}`
|
||||
const param = { stdio: 'inherit' }
|
||||
|
||||
createNew({ _dir: dir, _: [] })
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command, param)
|
||||
})
|
||||
|
||||
it('execute create mcfly with dir preferred over _dir', () => {
|
||||
const dir = 'preferred-dir'
|
||||
const command = `${baseCommand} ${dir}`
|
||||
const param = { stdio: 'inherit' }
|
||||
|
||||
createNew({ dir, _dir: 'not-preferred', _: [] })
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command, param)
|
||||
})
|
||||
|
||||
it('catch error', () => {
|
||||
const dir = 'fake-dir'
|
||||
const spy = vi.spyOn(consola, 'error')
|
||||
mocks.execSync.mockImplementationOnce(() => {
|
||||
throw new Error('hey')
|
||||
})
|
||||
|
||||
createNew({ dir, _: [] })
|
||||
|
||||
expect(spy).toHaveBeenCalledWith(new Error('hey'))
|
||||
})
|
|
@ -15,7 +15,7 @@ async function prepare(args: ParsedArgs) {
|
|||
// TODO: check for dir type (should be string)
|
||||
const dir: string = args.dir?.toString() || args._dir?.toString() || '.'
|
||||
const rootDir = resolve(dir)
|
||||
const [mcflyConfig] = await getMcFlyConfig()
|
||||
const { mcflyConfig } = await getMcFlyConfig()
|
||||
const nitroConfig = await getNitroConfig(mcflyConfig)
|
||||
const nitro = await createNitro({ rootDir, ...nitroConfig })
|
||||
|
||||
|
|
74
packages/core/src/cli/commands/serve.test.ts
Normal file
74
packages/core/src/cli/commands/serve.test.ts
Normal file
|
@ -0,0 +1,74 @@
|
|||
import consola from 'consola'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { exportedForTest } from './serve.js'
|
||||
|
||||
// describe.skip('FUNCTION: serve()', () => {
|
||||
// // // const testFn = exportedForTest.serve
|
||||
// // const mocks = vi.hoisted(() => {
|
||||
// // return {
|
||||
// // execSync: vi.fn(),
|
||||
// // }
|
||||
// // })
|
||||
// // vi.mock('node:child_process', () => {
|
||||
// // return {
|
||||
// // execSync: mocks.execSync,
|
||||
// // }
|
||||
// // })
|
||||
// // test('execute nitropack serve', async () => {
|
||||
// // const command = `npx nitropack dev`
|
||||
// // const param = { stdio: 'inherit' }
|
||||
// // testFn()
|
||||
// // expect(mocks.execSync).toHaveBeenCalledWith(command, param)
|
||||
// // })
|
||||
// // test('catch error', () => {
|
||||
// // const spy = vi.spyOn(consola, 'error')
|
||||
// // mocks.execSync.mockImplementationOnce(() => {
|
||||
// // throw new Error('hey')
|
||||
// // })
|
||||
// // testFn()
|
||||
// // expect(spy).toHaveBeenCalledWith(new Error('hey'))
|
||||
// // })
|
||||
// })
|
||||
|
||||
describe('fUNCTION: printInfo()', () => {
|
||||
const testFn = exportedForTest.printInfo
|
||||
|
||||
const createRequireMocks = vi.hoisted(() => {
|
||||
return {
|
||||
createRequire: vi.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('node:module', () => {
|
||||
return {
|
||||
createRequire: createRequireMocks.createRequire,
|
||||
}
|
||||
})
|
||||
|
||||
it('log mcfly and nitro versions', async () => {
|
||||
const spy = vi.spyOn(consola, 'log')
|
||||
createRequireMocks.createRequire.mockImplementationOnce(() => {
|
||||
return () => {
|
||||
return {
|
||||
version: '-1.0.0',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await testFn()
|
||||
|
||||
expect(spy.mock.calls[0][0]).toContain('McFly')
|
||||
expect(spy.mock.calls[0][0]).toContain('Nitro')
|
||||
})
|
||||
|
||||
it('catch error', async () => {
|
||||
createRequireMocks.createRequire.mockImplementationOnce(() => {
|
||||
throw new Error('error')
|
||||
})
|
||||
const spy = vi.spyOn(consola, 'error')
|
||||
|
||||
await testFn()
|
||||
|
||||
expect(spy).toHaveBeenCalledWith(new Error('error'))
|
||||
})
|
||||
})
|
|
@ -53,7 +53,7 @@ async function serve(args: ParsedArgs) {
|
|||
await nitro.close()
|
||||
}
|
||||
|
||||
const [mcflyConfig, appConfigFile] = await getMcFlyConfig()
|
||||
const { mcflyConfig, configFile } = await getMcFlyConfig()
|
||||
const nitroConfig = await getNitroConfig(mcflyConfig)
|
||||
|
||||
// create new nitro
|
||||
|
@ -92,7 +92,7 @@ async function serve(args: ParsedArgs) {
|
|||
}
|
||||
)
|
||||
nitro.hooks.hookOnce('restart', reload)
|
||||
nitro.options.runtimeConfig.appConfigFile = appConfigFile
|
||||
nitro.options.runtimeConfig.appConfigFile = configFile
|
||||
|
||||
nitro.options.handlers.push({
|
||||
middleware: true,
|
||||
|
|
|
@ -28,5 +28,5 @@ export async function getMcFlyConfig() {
|
|||
name: 'mcfly',
|
||||
})
|
||||
|
||||
return [mcflyConfig, configFile]
|
||||
return { mcflyConfig, configFile }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue