|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { existsSync } from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | +import chalk from 'chalk'; |
| 5 | + |
| 6 | +interface TestOptions { |
| 7 | + watch?: boolean; |
| 8 | + coverage?: boolean; |
| 9 | + ui?: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +export async function test(options: TestOptions) { |
| 13 | + const cwd = process.cwd(); |
| 14 | + |
| 15 | + console.log(chalk.blue('🧪 Running tests...\n')); |
| 16 | + |
| 17 | + // Check if the generated temp app exists |
| 18 | + const tmpDir = join(cwd, '.objectui-tmp'); |
| 19 | + const hasTempApp = existsSync(tmpDir); |
| 20 | + |
| 21 | + if (!hasTempApp) { |
| 22 | + throw new Error( |
| 23 | + 'No Object UI application found. Run \'objectui dev\' first to generate the application.' |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + // Check if package.json and node_modules exist |
| 28 | + const packageJsonPath = join(tmpDir, 'package.json'); |
| 29 | + const nodeModulesPath = join(tmpDir, 'node_modules'); |
| 30 | + |
| 31 | + if (!existsSync(packageJsonPath) || !existsSync(nodeModulesPath)) { |
| 32 | + throw new Error( |
| 33 | + 'Dependencies not installed. Run \'objectui dev\' first to set up the application.' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + let command = 'npx vitest'; |
| 39 | + |
| 40 | + if (options.watch) { |
| 41 | + command += ' --watch'; |
| 42 | + } else if (options.ui) { |
| 43 | + command += ' --ui'; |
| 44 | + } else { |
| 45 | + command += ' run'; |
| 46 | + } |
| 47 | + |
| 48 | + if (options.coverage) { |
| 49 | + command += ' --coverage'; |
| 50 | + } |
| 51 | + |
| 52 | + console.log(chalk.dim(` Running: ${command}\n`)); |
| 53 | + |
| 54 | + execSync(command, { |
| 55 | + cwd: tmpDir, |
| 56 | + stdio: 'inherit', |
| 57 | + }); |
| 58 | + |
| 59 | + if (!options.watch && !options.ui) { |
| 60 | + console.log(); |
| 61 | + console.log(chalk.green('✓ Tests completed successfully!')); |
| 62 | + console.log(); |
| 63 | + } |
| 64 | + } catch { |
| 65 | + // Vitest returns non-zero exit code when tests fail |
| 66 | + console.log(); |
| 67 | + console.log(chalk.red('✗ Some tests failed.')); |
| 68 | + console.log(); |
| 69 | + process.exit(1); |
| 70 | + } |
| 71 | +} |
0 commit comments