mcfly/packages/core/test/new.test.js
Ayo Ayco 73617647db
feat: move cli to core (#55)
* refactor: move cli to core

* feat: move cli to core

- use route-middleware in serve
- eliminate need for `routes` dir in app

* feat: use route-middleware in build

* chore: update test gh action
2025-01-08 21:21:31 +01:00

79 lines
1.8 KiB
JavaScript

import { expect, test, vi } from 'vitest'
import { exportedForTest } from '../cli/commands/new.mjs'
import { execSync } from 'node:child_process'
import consola from 'consola'
const testFn = 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,
}
})
test('execute create mcfly', () => {
const param = { stdio: 'inherit' }
testFn({ dir: undefined })
expect(execSync).toHaveBeenCalledWith(baseCommand, param)
})
test('execute create mcfly with no dir', () => {
const dir = 'fake-dir'
const command = `${baseCommand} ${dir}`
const param = { stdio: 'inherit' }
testFn({ dir: undefined })
expect(execSync).not.toHaveBeenCalledWith(command, param)
})
test('execute create mcfly with dir', () => {
const dir = 'fake-dir'
const command = `${baseCommand} ${dir}`
const param = { stdio: 'inherit' }
testFn({ dir })
expect(execSync).toHaveBeenCalledWith(command, param)
})
test('execute create mcfly with _dir', () => {
const dir = 'fake-dir'
const command = `${baseCommand} ${dir}`
const param = { stdio: 'inherit' }
testFn({ _dir: dir })
expect(execSync).toHaveBeenCalledWith(command, param)
})
test('execute create mcfly with dir preferred over _dir', () => {
const dir = 'preferred-dir'
const command = `${baseCommand} ${dir}`
const param = { stdio: 'inherit' }
testFn({ dir: dir, _dir: 'not-preferred' })
expect(execSync).toHaveBeenCalledWith(command, param)
})
test('catch error', () => {
const dir = 'fake-dir'
const spy = vi.spyOn(consola, 'error')
mocks.execSync.mockImplementationOnce(() => {
throw new Error('hey')
})
testFn({ dir })
expect(spy).toHaveBeenCalledWith(new Error('hey'))
})