
ν
μ€νΈ νμΌμμ Jestλ μ΄λ¬ν κ° λ©μλμ κ°μ²΄λ₯Ό μ μ νκ²½μ λ£λλ€. μ΄κ²λ€μ μ¬μ©νκΈ° μν΄ μ무κ²λ μꡬνκ±°λ κ°μ Έμ¬ νμκ° μλ€. κ·Έλ¬λ λͺ
μμ μΌλ‘ κ°μ Έμ€λ κ²μ μ νΈνλ κ²½μ° λ€μκ³Ό κ°μ΄ @jest/globalsμμ κ°μ Έμ¬ μ μλ€.
import {expect, jest, test} from '@jest/globals';
describe(name, fn)μ μ¬λ¬ κ΄λ ¨ ν
μ€νΈλ₯Ό ν¨κ» κ·Έλ£Ήννλ λΈλ‘μ μμ±νλ€. μλ₯Ό λ€μ΄ λ§μμ§λ§ μ λ§μ λμ§ μλ myBeverage κ°μ²΄κ° μλ κ²½μ° λ€μμ μ¬μ©νμ¬ ν
μ€νΈν μ μλ€.
const myBeverage = {
delicious: true,
sour: false,
};
describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy();
});
test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy();
});
});
describeμ νμ μ¬νμ΄ μλλ€. μ΅μμ μμ€(top level)μμ μ§μ ν
μ€νΈ λΈλ‘μ μμ±ν μ μλ€. νμ§λ§ ν
μ€νΈλ₯Ό κ·Έλ£ΉμΌλ‘ ꡬμ±νλ €λ 겨μ
νΈλ¦¬νλ€.
λν ν
μ€νΈ κ³μΈ΅μ΄ μλ κ²½μ° describe λΈλ‘μ μ€μ²©ν μλ μλ€.
class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}
const binaryStringToNumber = (binString) => {
if (!/^[01]+$/.test(binString)) {
throw new CustomError("Not a binary number.");
}
return parseInt(binString, 2);
};
describe("binaryStringToNumber", () => {
describe("given an invalid binary string", () => {
test("composed of non-numbers throws CustomError", () => {
// νλ κΉμ toThrow 볡μ΅λ νμ
expect(() => binaryStringToNumber("abc")).toThrow();
expect(() => binaryStringToNumber("abc")).toThrow(Error);
expect(() => binaryStringToNumber("abc")).toThrow(CustomError);
expect(() => binaryStringToNumber("abc")).toThrow("Not a");
expect(() => binaryStringToNumber("abc")).toThrow(/Not a binary/);
expect(() => binaryStringToNumber("abc")).toThrow(
/^Not a binary number.$/ // μ΄λ κ² μ κ·ννμμ μ°λ©΄ μ νν μ€λ₯ λ©μμ§λ₯Ό μΌμΉμμΌμΌ νλ€.
);
});
test("with extra whitespace throws CustomError", () => {
expect(() => binaryStringToNumber(" 100")).toThrow(CustomError);
});
});
describe("given a valid binary string", () => {
test("returns the correct number", () => {
expect(binaryStringToNumber("100")).toBe(4);
});
});
});
μ΄ κΈμ μλ μ¬μ΄νΈλ₯Ό μ°Έκ³ νμ¬ μμ±νμ΅λλ€π
https://jestjs.io/docs/api