auto-request
Version:
通过Yapi JSON Schema生成接口Axios或Taro接口
73 lines (59 loc) • 1.83 kB
JavaScript
/**
* 运行所有测试用例
* 使用方式:
* node scripts/run-tests.js # 使用源码测试 (tests/dev)
* node scripts/run-tests.js --built # 使用构建后的包测试 (tests/build)
*/
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const useBuilt = process.argv.includes('--built');
console.log(`\n🧪 运行测试用例 (${useBuilt ? '构建后的包' : '源码'})\n`);
// 根据模式选择测试目录
const testDir = useBuilt
? path.join(__dirname, '..', 'tests', 'build')
: path.join(__dirname, '..', 'tests', 'dev');
const fileExtension = useBuilt ? '.js' : '.ts';
// 自动扫描测试目录
const tests = [];
if (fs.existsSync(testDir)) {
const files = fs.readdirSync(testDir);
files.forEach((file) => {
if (file.endsWith(fileExtension)) {
const testName = file.replace(fileExtension, '');
tests.push({
name: testName,
script: useBuilt
? `./tests/build/${file}`
: `./tests/dev/${file}`,
});
}
});
}
let passCount = 0;
let failCount = 0;
console.log(`找到 ${tests.length} 个测试用例\n`);
tests.forEach((test) => {
console.log(`\n📝 测试: ${test.name}`);
try {
const cmd = useBuilt
? `node ${test.script}`
: `npx tsx ${test.script}`;
execSync(cmd, {
stdio: 'inherit',
cwd: path.join(__dirname, '..'),
env: { ...process.env, NODE_ENV: 'development' },
});
console.log(`✅ ${test.name} - 通过`);
passCount++;
} catch (error) {
console.log(`❌ ${test.name} - 失败`);
failCount++;
}
});
console.log(`\n${'='.repeat(50)}`);
console.log(`📊 测试结果: ${passCount} 通过, ${failCount} 失败`);
console.log(`${'='.repeat(50)}\n`);
if (failCount > 0) {
process.exit(1);
}