
π‘ TIP
npm install --save-dev @types/jestνμ μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νμ§ μλλΌλ @types/jestλ₯Ό μ€μΉνλ©΄ vscode μ½λ μλ μμ± κΈ°λ₯μ μ¬μ©ν μ μμ΅λλ€π
κ°μ ν μ€νΈνλ κ°μ₯ κ°λ¨ν λ°©λ²μ μ νν μΌμΉλ₯Ό μ¬μ©νλ κ²μ΄λ€.
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
μ μ½λμμ expect(2+2)λ "expectation" κ°μ²΄λ₯Ό λ°ννλ€. μΌλ°μ μΌλ‘ λ§€μ²λ₯Ό νΈμΆνλ κ²μ μ μΈνκ³ λ μ΄λ¬ν "expectation" κ°μ²΄λ‘ λ§μ μμ
μ μννμ§ μλλ€.
μ΄ μ½λμμ .toBe(4)λ λ§€μ²μ΄λ€.
Jestκ° μ€νλλ©΄ μ€ν¨ν λ§€μ²λ€μ λͺ¨λ μΆμ νμ¬ λ©μ§ μ€λ₯ λ©μμ§λ₯Ό μΆλ ₯ν΄μ€ μ μλ€.
.toBeλ Object.isλ₯Ό μ¬μ©νμ¬ μ νν μΌμΉλ₯Ό ν
μ€νΈνλ€.
κ°μ²΄μ κ°μ νμΈν λλ .toEqualλ₯Ό μ¬μ©νμ.
test('object assignment', () => {
const data = {one: 1};
data['two'] = 2;
expect(data).toEqual({one: 1, two: 2});
});
.toEqualμ κ°μ²΄ λλ λ°°μ΄μ λͺ¨λ νλλ₯Ό μ¬κ·μ μΌλ‘ κ²μ¬νλ€.
π‘ TIP
.toEqualμundefinedμμ±μ κ°μ§ κ°μ²΄μ ν€,undefinedμΈ λ°°μ΄ νλͺ©, ν¬μ λ°°μ΄(sparse array), κ°μ²΄ νμ λΆμΌμΉμΈ κ²½μ°λ 무μνλ€. μ΄λ₯Ό κ³ λ €νλ €λ©΄.toStrictEqualλ₯Ό μ¬μ©νμ.
notμ μ¬μ©νμ¬ λ§€μ²μ λ°λλ₯Ό ν
μ€νΈν μλ μλ€.
test('adding positive numbers is not zero', () => {
for (let a = 1; a < 10; a++) {
for (let b = 1; b < 10; b++) {
expect(a + b).not.toBe(0);
}
}
});
μ¬κΈ°μ notμ "Modifier" μ€ νλμΈλ° "Modifier"μ λ€μ κΈμμ μμ보μ
ν
μ€νΈμμ undefined, null κ·Έλ¦¬κ³ falseλ₯Ό ꡬλ³ν΄μΌνλ κ²½μ°λ μμ§λ§ μ΄λ₯Ό λ€λ₯΄κ² μ·¨κΈνκ³ μΆμ§ μμ κ²½μ°λ μλ€.
.toBeNullμ nullλ§ μΌμΉ.toBeUndefinedλ undefinedλ§ μΌμΉ.toBeDefinedλ toBeUndefinedμ λ°λ.toBeTruthyλ ifλ¬Έμ΄ trueλ‘ μ·¨κΈνλ λͺ¨λ νλͺ©κ³Ό μΌμΉ.toBeFalsyλ ifλ¬Έμ΄ falseλ‘ μ·¨κΈνλ λͺ¨λ νλͺ©κ³Ό μΌμΉμμλ λ€μκ³Ό κ°λ€
test("null", () => {
const n = null;
expect(n).toBeNull();
expect(n).toBeDefined();
expect(n).not.toBeUndefined();
expect(n).not.toBeTruthy();
expect(n).toBeFalsy();
});
test("zero", () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});
test("undefined", () => {
const u = undefined;
expect(u).not.toBeNull();
expect(u).not.toBeDefined();
expect(u).toBeUndefined();
expect(u).not.toBeTruthy();
expect(u).toBeFalsy();
});
μ«μλ₯Ό λΉκ΅νλ λλΆλΆμ λ°©λ²μλ μμνλ λ§€μ²κ° μλ€.
test("two plus two", () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThan(3.5);
expect(value).toBeGreaterThanOrEqual(4);
expect(value).toBeLessThan(5);
expect(value).toBeLessThan(4.5);
expect(value).toBeLessThanOrEqual(4);
// toBe and toEqual are equivalent for numbers
expect(value).toBe(4);
expect(value).toEqual(4);
});
ν
μ€νΈκ° μμ λ°μ¬λ¦Ό μ€λ₯(rounding error)μ μμ‘΄νλ κ²μ μνμ§ μμ κ²½μ° λΆλ μμμ (Floating point) λλ±μ±μ μν΄μ .toEqual λμ .toBeCloseToλ₯Ό μ¬μ©νμ.
test("adding floating point numbers", () => {
const value = 0.1 + 0.2;
// expect(value).toBe(0.3); // λ°μ¬λ¦Ό μ€λ₯(rounding error)λ‘ μΈν μ€λ₯
expect(value).toBeCloseTo(0.3); // this works
});
.toMatchλ₯Ό μ¬μ©ν΄μ μ κ·μμ λν λ¬Έμμ΄μ νμΈν μ μλ€.
test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});
.toContainμ μ¬μ©νμ¬ λ°°μ΄μ΄λ μ΄ν°λ¬λΈμ νΉμ νλͺ©μ΄ ν¬ν¨λμ΄ μλμ§ νμΈν μ μλ€. λ°°μ΄μ νλͺ©μ ν
μ€νΈνκΈ° μν΄ μ격ν λλ±μ±(equality) κ²μ¬μΈ ===λ₯Ό μ¬μ©νλ€.
const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'milk',
];
test('the shopping list has milk on it', () => {
expect(shoppingList).toContain('milk');
expect(new Set(shoppingList)).toContain('milk');
});
.toContainμ λ¬Έμμ΄μ λ€λ₯Έ λ¬Έμμ΄μ νμ λ¬Έμμ΄(substring)μΈμ§ μ¬λΆλ νμΈν μ μλ€.
test("'jerry' is a substring of 'my name is jerry'", () => {
expect("my name is jerry").toContain("jerry");
});
νΉμ ꡬ쑰μ κ°μ κ°μ§ νλͺ©μ΄ λ°°μ΄μ ν¬ν¨λμ΄ μλμ§ νμΈνλ €λ©΄ .toContainμ μ¬μ©νμ. λ°°μ΄μ νλͺ©μ ν
μ€νΈνκΈ° μν΄ μ΄ λ§€μ²λ κ°μ²΄ λμΌμ±(identity)λ₯Ό νμΈνλ λμ λͺ¨λ νλμ λλ±μ±(equality)μ μ¬κ·μ μΌλ‘ νμΈνλ€.
function myBeverages() {
return [
"cocacola",
"sprite",
{ spicy: true, sweet: true },
{ delicious: true, sour: false },
];
}
describe("my beverage", () => {
test("is delicious and not sour", () => {
const myBeverage = { delicious: true, sour: false };
expect(myBeverages()).toContainEqual(myBeverage, "cocacola");
});
});
νΉμ ν¨μκ° νΈμΆλ λ μ€λ₯λ₯Ό λ°μμν€λμ§ ν
μ€νΈνλ €λ©΄ .toThrowλ₯Ό μ¬μ©νμ
function compileAndroidCode() {
throw new Error("you are using the wrong JDK!");
}
test("compiling android goes as expected", () => {
expect(() => compileAndroidCode()).toThrow();
expect(() => compileAndroidCode()).toThrow(Error);
// μ€λ₯ λ©μμ§μ ν¬ν¨λμ΄μΌ νλ λ¬Έμμ΄ λλ μ κ· ννμμ μ¬μ©ν μ λ μλ€.
expect(() => compileAndroidCode()).toThrow("you are using the wrong");
expect(() => compileAndroidCode()).toThrow(/JDK/);
// μλμ κ°μ μ κ· ννμμ μ¬μ©νλ©΄ μ νν μ€λ₯ λ©μμ§λ₯Ό μΌμΉμν¬ μ μλ€.
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
});
π‘TIP
μμΈλ₯Ό λ°μμν€λ ν¨μλ wrapping function λ΄μμ νΈμΆλμ΄μΌ νλ€. κ·Έλ μ§ μμΌλ©΄.toTrowλ μ€ν¨νλ€.
μμ§κΉμ§λ λ§λ³΄κΈ°μλ€. λ§€μ²μ μ 체 λͺ©λ‘μ μ°Έμ‘° λ¬Έμλ₯Ό νμΈνμ.
μ¬μ© κ°λ₯ν λ§€μ²μ λν΄ λ°°μ λ€λ©΄ λ€μ λ¨κ³λ Jestμμ λΉλκΈ° μ½λλ₯Ό ν
μ€νΈνλ λ°©λ²μ νμΈνλ κ²μ΄λ€. λ€μ κΈμμ μ΄ν΄λ³΄μ!
μ΄ κΈμ μλ μ¬μ΄νΈλ₯Ό μ°Έκ³ νμ¬ μμ±νμ΅λλ€π
https://jestjs.io/docs/using-matchers