legendaryjs
Version:
LegendaryJS โ The ultimate backend framework for speed, power, and simplicity.
51 lines (43 loc) โข 1.71 kB
JavaScript
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;