qapinterface
Version:
Comprehensive API utilities for Node.js applications including authentication, security, request processing, and response handling with zero external dependencies
68 lines (62 loc) • 1.7 kB
JavaScript
class TestRunner {
constructor() {
this.tests = [];
this.results = { passed: 0, failed: 0, errors: [] };
}
describe(description, callback) {
console.log(`\n=== ${description} ===`);
callback();
}
it(description, callback) {
this.tests.push({ description, callback });
}
async run() {
for (const test of this.tests) {
try {
await test.callback();
this.results.passed++;
console.log(`✓ ${test.description}`);
} catch (error) {
this.results.failed++;
this.results.errors.push({ description: test.description, error: error.message });
console.log(`✗ ${test.description} - ${error.message}`);
}
}
}
expect(actual) {
return {
to: {
equal: (expected) => {
if (actual !== expected) {
throw new Error(`Expected ${expected}, got ${actual}`);
}
},
be: {
true: () => {
if (actual !== true) {
throw new Error(`Expected true, got ${actual}`);
}
},
false: () => {
if (actual !== false) {
throw new Error(`Expected false, got ${actual}`);
}
}
}
}
};
}
summary() {
console.log(`\n=== Test Summary ===`);
console.log(`Passed: ${this.results.passed}`);
console.log(`Failed: ${this.results.failed}`);
if (this.results.errors.length > 0) {
console.log(`\nErrors:`);
this.results.errors.forEach(({ description, error }) => {
console.log(` - ${description}: ${error}`);
});
}
return this.results.failed === 0;
}
}
module.exports = TestRunner;