mcfly/packages/cli/test/build.test.js
2024-12-19 23:11:07 +01:00

46 lines
926 B
JavaScript

import consola from 'consola'
import { vi, expect, test } from 'vitest'
import { exportedForTest } from '../commands/build.mjs'
const testFn = exportedForTest.build
const mocks = vi.hoisted(() => {
return {
execSync: vi.fn(),
}
})
vi.mock('node:child_process', () => {
return {
execSync: mocks.execSync,
}
})
test('start build with message', () => {
const message = 'Building project...'
const spy = vi.spyOn(consola, 'start')
testFn()
expect(spy).toHaveBeenCalledWith(message)
})
test('execute nitropack build', () => {
const command = 'npx nitropack build'
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'))
})