From 9899a25d9d8b1943dc63eb36416e204a38dc6ca1 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sat, 22 Mar 2025 21:17:45 +0100 Subject: [PATCH] test: organize test files --- packages/config/src/index.ts | 15 ---- packages/config/src/nitro-config.ts | 36 --------- packages/core/test/build.test.js | 44 ----------- .../{src/cli/commands => test}/build.test.ts | 2 +- packages/core/test/generate.test.js | 15 ---- .../cli/commands => test}/generate.test.ts | 2 +- packages/core/test/new.test.js | 79 ------------------- .../{src/cli/commands => test}/new.test.ts | 2 +- packages/core/test/prepare.test.js | 50 ------------ .../cli/commands => test}/prepare.test.ts | 2 +- packages/core/test/serve.test.js | 74 ----------------- .../{src/cli/commands => test}/serve.test.ts | 2 +- packages/core/tsconfig.json | 9 ++- 13 files changed, 13 insertions(+), 319 deletions(-) delete mode 100644 packages/config/src/nitro-config.ts delete mode 100644 packages/core/test/build.test.js rename packages/core/{src/cli/commands => test}/build.test.ts (93%) delete mode 100644 packages/core/test/generate.test.js rename packages/core/{src/cli/commands => test}/generate.test.ts (82%) delete mode 100644 packages/core/test/new.test.js rename packages/core/{src/cli/commands => test}/new.test.ts (96%) delete mode 100644 packages/core/test/prepare.test.js rename packages/core/{src/cli/commands => test}/prepare.test.ts (93%) delete mode 100644 packages/core/test/serve.test.js rename packages/core/{src/cli/commands => test}/serve.test.ts (96%) diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index 1fd3829..c77d646 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -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' diff --git a/packages/config/src/nitro-config.ts b/packages/config/src/nitro-config.ts deleted file mode 100644 index b1b173a..0000000 --- a/packages/config/src/nitro-config.ts +++ /dev/null @@ -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'], - }, - ], - }, -} diff --git a/packages/core/test/build.test.js b/packages/core/test/build.test.js deleted file mode 100644 index 0a878c7..0000000 --- a/packages/core/test/build.test.js +++ /dev/null @@ -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')) -// }) diff --git a/packages/core/src/cli/commands/build.test.ts b/packages/core/test/build.test.ts similarity index 93% rename from packages/core/src/cli/commands/build.test.ts rename to packages/core/test/build.test.ts index fdc9d93..b851a22 100644 --- a/packages/core/src/cli/commands/build.test.ts +++ b/packages/core/test/build.test.ts @@ -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 diff --git a/packages/core/test/generate.test.js b/packages/core/test/generate.test.js deleted file mode 100644 index 69e2c58..0000000 --- a/packages/core/test/generate.test.js +++ /dev/null @@ -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') -}) diff --git a/packages/core/src/cli/commands/generate.test.ts b/packages/core/test/generate.test.ts similarity index 82% rename from packages/core/src/cli/commands/generate.test.ts rename to packages/core/test/generate.test.ts index ef77d66..8add439 100644 --- a/packages/core/src/cli/commands/generate.test.ts +++ b/packages/core/test/generate.test.ts @@ -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 diff --git a/packages/core/test/new.test.js b/packages/core/test/new.test.js deleted file mode 100644 index 333a013..0000000 --- a/packages/core/test/new.test.js +++ /dev/null @@ -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')) -}) diff --git a/packages/core/src/cli/commands/new.test.ts b/packages/core/test/new.test.ts similarity index 96% rename from packages/core/src/cli/commands/new.test.ts rename to packages/core/test/new.test.ts index cd5db22..27bcc4d 100644 --- a/packages/core/src/cli/commands/new.test.ts +++ b/packages/core/test/new.test.ts @@ -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 diff --git a/packages/core/test/prepare.test.js b/packages/core/test/prepare.test.js deleted file mode 100644 index cef34c5..0000000 --- a/packages/core/test/prepare.test.js +++ /dev/null @@ -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() -}) diff --git a/packages/core/src/cli/commands/prepare.test.ts b/packages/core/test/prepare.test.ts similarity index 93% rename from packages/core/src/cli/commands/prepare.test.ts rename to packages/core/test/prepare.test.ts index 09188e8..4a2e160 100644 --- a/packages/core/src/cli/commands/prepare.test.ts +++ b/packages/core/test/prepare.test.ts @@ -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(() => { diff --git a/packages/core/test/serve.test.js b/packages/core/test/serve.test.js deleted file mode 100644 index 62ee499..0000000 --- a/packages/core/test/serve.test.js +++ /dev/null @@ -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')) - }) -}) diff --git a/packages/core/src/cli/commands/serve.test.ts b/packages/core/test/serve.test.ts similarity index 96% rename from packages/core/src/cli/commands/serve.test.ts rename to packages/core/test/serve.test.ts index bc70d91..a04c722 100644 --- a/packages/core/src/cli/commands/serve.test.ts +++ b/packages/core/test/serve.test.ts @@ -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 diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 8756fd8..2b232f9 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -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,