test: organize test files

This commit is contained in:
Ayo Ayco 2025-03-22 21:17:45 +01:00
parent b2ee0f35bb
commit 9899a25d9d
13 changed files with 13 additions and 319 deletions

View file

@ -1,16 +1 @@
import { nitroConfig } from './nitro-config.js'
import type { NitroConfig } from 'nitropack'
/**
* Returns the Nitro configuration for a McFly project
* @returns {NitroConfig}
*/
export default function (): NitroConfig {
return nitroConfig
}
export { defineMcFlyConfig } from './define-mcfly-config.js'
export type { McFlyConfig } from './define-mcfly-config.js'
export const hello = 'world'

View file

@ -1,36 +0,0 @@
import type { NitroConfig } from 'nitropack'
/**
* @typedef {import('nitropack').NitroConfig} NitroConfig
* @type {NitroConfig}
*/
export const nitroConfig: NitroConfig = {
framework: {
name: 'McFly',
},
compatibilityDate: '2024-12-08',
srcDir: 'src',
apiDir: 'api',
buildDir: '.mcfly',
devServer: {
watch: ['./pages', './components', './api'],
},
serverAssets: [
{
baseName: 'pages',
dir: './pages',
},
{
baseName: 'components',
dir: './components',
},
],
imports: {
presets: [
{
from: 'web-component-base',
imports: ['WebComponent', 'html', 'attachEffect'],
},
],
},
}

View file

@ -1,44 +0,0 @@
import consola from 'consola'
import { vi, expect, test } from 'vitest'
import { exportedForTest } from '../src/cli/commands/build'
const testFn = exportedForTest.build
const mocks = vi.hoisted(() => {
return {
build: vi.fn(),
}
})
vi.mock('nitropack', () => {
return {
build: mocks.build,
}
})
test('start build with message', () => {
const message = 'Building project...'
const spy = vi.spyOn(consola, 'start')
testFn()
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'))
// })

View file

@ -1,7 +1,7 @@
import type { ParsedArgs } from 'citty'
import consola from 'consola'
import { expect, it, vi } from 'vitest'
import { exportedForTest } from './build.js'
import { exportedForTest } from '../src/cli/commands/build.js'
const build = exportedForTest.build

View file

@ -1,15 +0,0 @@
import { expect, test, vi } from 'vitest'
import { exportedForTest } from '../src/cli/commands/generate'
import consola from 'consola'
const testFn = exportedForTest.generate
test('show box message in-progress', () => {
const spy = vi.spyOn(consola, 'box')
testFn()
const arg = spy.mock.calls[0][0]
expect(spy).toHaveBeenCalled()
expect(arg).not.toContain('In-progress')
})

View file

@ -1,5 +1,5 @@
import { expect, it, vi } from 'vitest'
import { exportedForTest } from './generate.js'
import { exportedForTest } from '../src/cli/commands/generate.js'
import consola from 'consola'
const generate = exportedForTest.generate

View file

@ -1,79 +0,0 @@
import { expect, test, vi } from 'vitest'
import { exportedForTest } from '../src/cli/commands/new'
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'))
})

View file

@ -1,7 +1,7 @@
import { execSync } from 'node:child_process'
import { consola } from 'consola'
import { expect, vi } from 'vitest'
import { exportedForTest } from './new.js'
import { exportedForTest } from '../src/cli/commands/new.js'
import { it } from 'node:test'
const createNew = exportedForTest.createNew

View file

@ -1,50 +0,0 @@
import { test, expect, vi } from 'vitest'
import { exportedForTest } from '../src/cli/commands/prepare'
import consola from 'consola'
import { execSync } from 'node:child_process'
const testFn = exportedForTest.prepare
const mocks = vi.hoisted(() => {
return {
execSync: vi.fn(),
}
})
vi.mock('node:child_process', () => {
return {
execSync: mocks.execSync,
}
})
test('start prepare script', () => {
const spy = vi.spyOn(consola, 'start')
testFn()
expect(spy).toHaveBeenCalled()
})
test.skip('execute nitropack prepare', () => {
const successSpy = vi.spyOn(consola, 'success')
const command = 'npx nitropack prepare'
const param = { stdio: 'inherit' }
testFn()
expect(execSync).toHaveBeenCalledWith(command, param)
expect(successSpy).toHaveBeenCalled()
})
test('catch error', () => {
const errSpy = vi.spyOn(consola, 'error')
const failSpy = vi.spyOn(consola, 'fail')
mocks.execSync.mockImplementationOnce(() => {
throw new Error()
})
testFn()
expect(errSpy).toHaveBeenCalled()
expect(failSpy).toHaveBeenCalled()
})

View file

@ -1,6 +1,6 @@
import { consola } from 'consola'
import { it, expect, vi } from 'vitest'
import { exportedForTest } from './prepare.js'
import { exportedForTest } from '../src/cli/commands/prepare.js'
const prepare = exportedForTest.prepare
const mocks = vi.hoisted(() => {

View file

@ -1,74 +0,0 @@
import { describe, expect, test, vi } from 'vitest'
import { exportedForTest } from '../src/cli/commands/serve'
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'))
})
})

View file

@ -1,6 +1,6 @@
import consola from 'consola'
import { describe, expect, it, vi } from 'vitest'
import { exportedForTest } from './serve.js'
import { exportedForTest } from '../src/cli/commands/serve.js'
// describe.skip('FUNCTION: serve()', () => {
// // // const testFn = exportedForTest.serve

View file

@ -1,6 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src"],
"include": [
"src",
"test/build.test.ts",
"test/generate.test.ts",
"test/new.test.ts",
"test/prepare.test.ts",
"test/serve.test.ts"
],
"compilerOptions": {
"allowJs": true,
"emitDeclarationOnly": false,