Javascript - TDD (0)

Moonshot·2022년 4월 23일
0
	const sum = (a, b) => a + b;
    const subtract = (a, b) => a - b;

// if(result !== expected) {
//     throw new Error(`Expected ${expected}, but got ${result}`);
// }

test('sum', () => {
    const result = sum(1, 2);
    const expected = 3;
    expect(result).toBe(expected);
})

test('subtract', () => {
    const result = subtract(1, 2);
    const expected = -1;
    expect(result).toBe(expected);
})


function test(title, callback) {
    try{
        callback();
        console.log(`%c${title}`, 'color: green');
    }
    catch(e) {
        console.log(`%c${title}`, 'color: red');
        console.error(e);
    }
}

function expect(actual) {
    return {
        toBe(expected) {
            if(actual !== expected) {
                throw new Error(`Expected ${expected}, but got ${actual}`);
            }
        }
    }
}
profile
Jidoil

0개의 댓글