mcfly/packages/core/test/serve.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

74 lines
1.9 KiB
JavaScript

import { describe, expect, test, vi } from 'vitest'
import { exportedForTest } from '../cli/commands/serve.mjs'
import consola from 'consola'
// 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,
}
})
test('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')
})
test('catch error', async () => {
createRequireMocks.createRequire.mockImplementationOnce(() => {
throw new Error('error')
})
const spy = vi.spyOn(consola, 'error')
await testFn()
expect(spy).toHaveBeenCalledWith(new Error('error'))
})
})