| 1 |
|
| 2 |
* Minimal test runner for BB frontend JS tests. |
| 3 |
* No npm dependencies — runs with plain Node.js. |
| 4 |
* |
| 5 |
* Usage: node src-tauri/frontend/js/tests/run.js |
| 6 |
|
| 7 |
|
| 8 |
let passed = 0; |
| 9 |
let failed = 0; |
| 10 |
let currentSuite = ''; |
| 11 |
const failures = []; |
| 12 |
|
| 13 |
function describe(name, fn) { |
| 14 |
currentSuite = name; |
| 15 |
fn(); |
| 16 |
currentSuite = ''; |
| 17 |
} |
| 18 |
|
| 19 |
function test(name, fn) { |
| 20 |
const fullName = currentSuite ? `${currentSuite} > ${name}` : name; |
| 21 |
try { |
| 22 |
fn(); |
| 23 |
passed++; |
| 24 |
} catch (err) { |
| 25 |
failed++; |
| 26 |
failures.push({ name: fullName, error: err.message }); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
function assert(condition, message) { |
| 31 |
if (!condition) throw new Error(message || 'Assertion failed'); |
| 32 |
} |
| 33 |
|
| 34 |
function assertEqual(actual, expected, message) { |
| 35 |
if (actual !== expected) { |
| 36 |
throw new Error( |
| 37 |
message || |
| 38 |
`Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}` |
| 39 |
); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
function assertDeepEqual(actual, expected, message) { |
| 44 |
const a = JSON.stringify(actual); |
| 45 |
const e = JSON.stringify(expected); |
| 46 |
if (a !== e) { |
| 47 |
throw new Error(message || `Expected ${e}, got ${a}`); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
function assertThrows(fn, message) { |
| 52 |
let threw = false; |
| 53 |
try { fn(); } catch (_) { threw = true; } |
| 54 |
if (!threw) throw new Error(message || 'Expected function to throw'); |
| 55 |
} |
| 56 |
|
| 57 |
function report() { |
| 58 |
console.log(`\n${passed + failed} tests: ${passed} passed, ${failed} failed`); |
| 59 |
if (failures.length > 0) { |
| 60 |
console.log('\nFailures:'); |
| 61 |
failures.forEach(f => { |
| 62 |
console.log(` FAIL: ${f.name}`); |
| 63 |
console.log(` ${f.error}`); |
| 64 |
}); |
| 65 |
} |
| 66 |
return failed === 0; |
| 67 |
} |
| 68 |
|
| 69 |
module.exports = { describe, test, assert, assertEqual, assertDeepEqual, assertThrows, report }; |
| 70 |
|