|
| 1 | +import { Command } from 'commander' |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 3 | + |
| 4 | +vi.mock('node:child_process', () => ({ |
| 5 | + spawn: vi.fn(), |
| 6 | +})) |
| 7 | + |
| 8 | +import { createCommand } from './index.js' |
| 9 | + |
| 10 | +describe('createCommand', () => { |
| 11 | + let program: Command |
| 12 | + let consoleErrorSpy: ReturnType<typeof vi.spyOn> |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + program = new Command() |
| 16 | + createCommand(program) |
| 17 | + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 18 | + vi.clearAllMocks() |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + consoleErrorSpy.mockRestore() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should spawn npm create hono@latest with no arguments', async () => { |
| 26 | + const { spawn } = await import('node:child_process') |
| 27 | + const mockChildProcess = { |
| 28 | + on: vi.fn((event, callback) => { |
| 29 | + if (event === 'exit') { |
| 30 | + callback(0) |
| 31 | + } |
| 32 | + return mockChildProcess |
| 33 | + }), |
| 34 | + } |
| 35 | + vi.mocked(spawn).mockReturnValue(mockChildProcess as never) |
| 36 | + |
| 37 | + await program.parseAsync(['node', 'test', 'create']) |
| 38 | + |
| 39 | + // Currently, supporting only npm |
| 40 | + expect(spawn).toHaveBeenCalledWith('npm', ['create', 'hono@latest'], { |
| 41 | + stdio: 'inherit', |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should spawn npm create hono@latest with target directory', async () => { |
| 46 | + const { spawn } = await import('node:child_process') |
| 47 | + const mockChildProcess = { |
| 48 | + on: vi.fn((event, callback) => { |
| 49 | + if (event === 'exit') { |
| 50 | + callback(0) |
| 51 | + } |
| 52 | + return mockChildProcess |
| 53 | + }), |
| 54 | + } |
| 55 | + vi.mocked(spawn).mockReturnValue(mockChildProcess as never) |
| 56 | + |
| 57 | + await program.parseAsync(['node', 'test', 'create', 'my-app']) |
| 58 | + |
| 59 | + expect(spawn).toHaveBeenCalledWith('npm', ['create', 'hono@latest', 'my-app'], { |
| 60 | + stdio: 'inherit', |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should handle spawn error gracefully', async () => { |
| 65 | + const { spawn } = await import('node:child_process') |
| 66 | + const mockChildProcess = { |
| 67 | + on: vi.fn((event, callback) => { |
| 68 | + if (event === 'error') { |
| 69 | + callback(new Error('spawn ENOENT')) |
| 70 | + } |
| 71 | + return mockChildProcess |
| 72 | + }), |
| 73 | + } |
| 74 | + vi.mocked(spawn).mockReturnValue(mockChildProcess as never) |
| 75 | + |
| 76 | + await expect(program.parseAsync(['node', 'test', 'create'])).rejects.toThrow( |
| 77 | + 'Failed to execute npm: spawn ENOENT' |
| 78 | + ) |
| 79 | + |
| 80 | + expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to execute npm: spawn ENOENT') |
| 81 | + }) |
| 82 | + |
| 83 | + it('should throw error when npm exits with non-zero code', async () => { |
| 84 | + const { spawn } = await import('node:child_process') |
| 85 | + const mockChildProcess = { |
| 86 | + on: vi.fn((event, callback) => { |
| 87 | + if (event === 'exit') { |
| 88 | + callback(1) |
| 89 | + } |
| 90 | + return mockChildProcess |
| 91 | + }), |
| 92 | + } |
| 93 | + vi.mocked(spawn).mockReturnValue(mockChildProcess as never) |
| 94 | + |
| 95 | + await expect(program.parseAsync(['node', 'test', 'create'])).rejects.toThrow( |
| 96 | + 'npm create hono@latest exited with code 1' |
| 97 | + ) |
| 98 | + }) |
| 99 | +}) |
0 commit comments