UNPKG

legendaryjs

Version:

LegendaryJS โ€“ The ultimate backend framework for speed, power, and simplicity.

51 lines (43 loc) โ€ข 1.71 kB
const { execSync } = require('child_process'); const chalk = require('chalk'); const fs = require('fs'); const path = require('path'); function generateTestScaffold() { const routePath = path.join(process.cwd(), 'routes/api.v1.json'); const outputPath = path.join(process.cwd(), '__tests__/generated.test.js'); if (!fs.existsSync(routePath)) return console.log('โŒ No routes found to test.'); const { routes } = JSON.parse(fs.readFileSync(routePath)); if (!routes || routes.length === 0) return console.log('โŒ No routes to scaffold.'); const lines = [ "const request = require('supertest');", "const app = require('../app');", "describe('๐Ÿงช Legendary API Auto Tests', () => {" ]; routes.forEach(r => { lines.push(` it('${r.method || 'GET'} ${r.path}', async () => {`); lines.push(` const res = await request(app).${(r.method || 'get').toLowerCase()}('${r.path}');`); lines.push(` expect(res.statusCode).toBe(200);`); lines.push(" });"); }); lines.push("});"); fs.writeFileSync(outputPath, lines.join('\n')); console.log(chalk.green(`โœ… Tests generated at: ${outputPath}`)); } function runTests(subCommand) { if (subCommand === 'generate') { generateTestScaffold(); } else { try { const jestExists = fs.existsSync('./jest.config.js'); if (jestExists) { console.log('๐Ÿงช Running Jest tests...'); execSync('npx jest', { stdio: 'inherit' }); } else { console.log(chalk.yellow('โš ๏ธ Jest config not found.')); } } catch (err) { console.error(chalk.red('โŒ Test run failed.')); } } } module.exports = runTests;