Jest — популярний тестовий фреймворк від Meta (Facebook).
const clamp = (v, min, max) => Math.min(Math.max(v, min), max);
// Симуляція тестів без Jest
const tests = [
{ input: [5, 0, 10], expected: 5 },
{ input: [-5, 0, 10], expected: 0 },
{ input: [15, 0, 10], expected: 10 },
];
let passed = 0;
tests.forEach(({input, expected}) => {
const result = clamp(...input);
const ok = result === expected;
if (ok) passed++;
console.log(`clamp(${input}) → ${result} ${ok ? 'PASS' : 'FAIL'}`);
});
console.log(`\n${passed}/${tests.length} тестів пройдено`);
const unique = arr => [...new Set(arr)];
const tests = [
{ input: [[1,2,2,3,3,3]], expected: [1,2,3] },
{ input: [['a','b','a','c']], expected: ['a','b','c'] },
{ input: [[]], expected: [] },
];
let passed = 0;
tests.forEach(({input, expected}) => {
const result = unique(...input);
const ok = JSON.stringify(result) === JSON.stringify(expected);
if (ok) passed++;
console.log(`unique(${JSON.stringify(input[0])}) → ${JSON.stringify(result)} ${ok ? 'PASS' : 'FAIL'}`);
});
console.log(`\n${passed}/${tests.length} тестів пройдено`);
function divide(a, b) {
if (b === 0) throw new Error('Ділення на нуль');
return a / b;
}
function runTest(name, fn) {
try {
fn();
console.log(`${name} → PASS`);
} catch (e) {
console.log(`${name} → FAIL: ${e.message}`);
}
}
runTest('divide(10,2) === 5', () => { if (divide(10,2) !== 5) throw new Error('Невірно'); });
runTest('divide(7,3) ≈ 2.33', () => { if (divide(7,3).toFixed(2) !== '2.33') throw new Error('Невірно'); });
runTest('divide(5,0) throws', () => { try { divide(5,0); throw new Error('Не кинуло'); } catch(e) { if (!e.message.includes('нуль')) throw e; } });
runTest('divide(0,5) === 0', () => { if (divide(0,5) !== 0) throw new Error('Невірно'); });
console.log('\n4/4 тестів пройдено');