diff --git a/packages/core/src/cli/commands/build.test.ts b/packages/core/src/cli/commands/build.test.ts new file mode 100644 index 0000000..fdc9d93 --- /dev/null +++ b/packages/core/src/cli/commands/build.test.ts @@ -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')) +// }) diff --git a/packages/core/src/cli/commands/build.ts b/packages/core/src/cli/commands/build.ts index 73746d1..e088e73 100644 --- a/packages/core/src/cli/commands/build.ts +++ b/packages/core/src/cli/commands/build.ts @@ -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) diff --git a/packages/core/src/cli/commands/generate.test.ts b/packages/core/src/cli/commands/generate.test.ts new file mode 100644 index 0000000..ef77d66 --- /dev/null +++ b/packages/core/src/cli/commands/generate.test.ts @@ -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') +}) diff --git a/packages/core/src/cli/commands/new.test.ts b/packages/core/src/cli/commands/new.test.ts new file mode 100644 index 0000000..cd5db22 --- /dev/null +++ b/packages/core/src/cli/commands/new.test.ts @@ -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')) +}) diff --git a/packages/core/src/cli/commands/prepare.ts b/packages/core/src/cli/commands/prepare.ts index f2763da..7ec9558 100755 --- a/packages/core/src/cli/commands/prepare.ts +++ b/packages/core/src/cli/commands/prepare.ts @@ -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 }) diff --git a/packages/core/src/cli/commands/serve.test.ts b/packages/core/src/cli/commands/serve.test.ts new file mode 100644 index 0000000..bc70d91 --- /dev/null +++ b/packages/core/src/cli/commands/serve.test.ts @@ -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')) + }) +}) diff --git a/packages/core/src/cli/commands/serve.ts b/packages/core/src/cli/commands/serve.ts index 3aaced4..9f4d8d0 100644 --- a/packages/core/src/cli/commands/serve.ts +++ b/packages/core/src/cli/commands/serve.ts @@ -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, diff --git a/packages/core/src/get-config.js b/packages/core/src/get-config.js index 624b7bf..28fc84a 100644 --- a/packages/core/src/get-config.js +++ b/packages/core/src/get-config.js @@ -28,5 +28,5 @@ export async function getMcFlyConfig() { name: 'mcfly', }) - return [mcflyConfig, configFile] + return { mcflyConfig, configFile } }