[JEST μž…λ¬Έν•˜κΈ° 3] πŸ‘©πŸ»β€πŸ”§λ§€μ²˜ μ‚¬μš©ν•˜κΈ°πŸ‘©πŸ»β€πŸ”§

JerryΒ·2023λ…„ 8μ›” 7일

Jest

λͺ©λ‘ 보기
3/10
post-thumbnail

πŸ‘©πŸ»β€πŸ”§ Using Matchers

  • JestλŠ” "맀처"λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ 값을 ν…ŒμŠ€νŠΈν•  수 μžˆλ‹€.
  • μ—¬κΈ°μ„œλŠ” 일반적으둜 μ‚¬μš©λ˜λŠ” λͺ‡κ°€μ§€ 맀처λ₯Ό μ†Œκ°œν•œλ‹€.
  • 전체 λͺ©λ‘μ€ expect API docλ₯Ό μ°Έκ³ ν•˜μž

πŸ’‘ TIP

npm install --save-dev @types/jest

νƒ€μž…μŠ€ν¬λ¦½νŠΈλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šλ”λΌλ„ @types/jestλ₯Ό μ„€μΉ˜ν•˜λ©΄ vscode μ½”λ“œ μžλ™ μ™„μ„± κΈ°λŠ₯을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€πŸ˜‰


πŸ˜€ 곡톡 맀처(Matchers)


.toBe

값을 ν…ŒμŠ€νŠΈν•˜λŠ” κ°€μž₯ κ°„λ‹¨ν•œ 방법은 μ •ν™•ν•œ 일치λ₯Ό μ‚¬μš©ν•˜λŠ” 것이닀.

test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});

μœ„ μ½”λ“œμ—μ„œ expect(2+2)λŠ” "expectation" 객체λ₯Ό λ°˜ν™˜ν•œλ‹€. 일반적으둜 맀처λ₯Ό ν˜ΈμΆœν•˜λŠ” 것을 μ œμ™Έν•˜κ³ λŠ” μ΄λŸ¬ν•œ "expectation" 객체둜 λ§Žμ€ μž‘μ—…μ„ μˆ˜ν–‰ν•˜μ§€ μ•ŠλŠ”λ‹€.

이 μ½”λ“œμ—μ„œ .toBe(4)λŠ” λ§€μ²˜μ΄λ‹€.
Jestκ°€ μ‹€ν–‰λ˜λ©΄ μ‹€νŒ¨ν•œ λ§€μ²˜λ“€μ„ λͺ¨λ‘ μΆ”μ ν•˜μ—¬ λ©‹μ§„ 였λ₯˜ λ©”μ‹œμ§€λ₯Ό 좜λ ₯해쀄 수 μžˆλ‹€.

.toBeλŠ” Object.isλ₯Ό μ‚¬μš©ν•˜μ—¬ μ •ν™•ν•œ 일치λ₯Ό ν…ŒμŠ€νŠΈν•œλ‹€.
객체의 값을 확인할 λ•ŒλŠ” .toEqualλ₯Ό μ‚¬μš©ν•˜μž.


.toEqual, .toStrictEqual

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"은 λ‹€μŒ κΈ€μ—μ„œ μ•Œμ•„λ³΄μž


πŸ€₯ Truthiness


ν…ŒμŠ€νŠΈμ—μ„œ 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();
});

πŸ”’ 숫자(Numbers)


숫자λ₯Ό λΉ„κ΅ν•˜λŠ” λŒ€λΆ€λΆ„μ˜ λ°©λ²•μ—λŠ” μƒμ‘ν•˜λŠ” λ§€μ²˜κ°€ μžˆλ‹€.

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
});

πŸ”‘ λ¬Έμžμ—΄(Strings)


.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/);
});

πŸ…°οΈ λ°°μ—΄(Arrays)κ³Ό μ΄ν„°λŸ¬λΈ”(iterables)


.toContain(item)

.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");
});

.toContainEqual(item)

νŠΉμ • ꡬ쑰와 값을 κ°€μ§„ ν•­λͺ©μ΄ 배열에 ν¬ν•¨λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•˜λ €λ©΄ .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");
  });
});

⚠️ μ˜ˆμ™Έ(Exeptions)


νŠΉμ • ν•¨μˆ˜κ°€ 호좜될 λ•Œ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚€λŠ”μ§€ ν…ŒμŠ€νŠΈν•˜λ €λ©΄ .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λŠ” μ‹€νŒ¨ν•œλ‹€.


βž• And More


μ•„μ§κΉŒμ§€λŠ” λ§›λ³΄κΈ°μ˜€λ‹€. 맀처의 전체 λͺ©λ‘μ€ μ°Έμ‘° λ¬Έμ„œλ₯Ό ν™•μΈν•˜μž.
μ‚¬μš© κ°€λŠ₯ν•œ λ§€μ²˜μ— λŒ€ν•΄ λ°°μ› λ‹€λ©΄ λ‹€μŒ λ‹¨κ³„λŠ” Jestμ—μ„œ 비동기 μ½”λ“œλ₯Ό ν…ŒμŠ€νŠΈν•˜λŠ” 방법을 ν™•μΈν•˜λŠ” 것이닀. λ‹€μŒ κΈ€μ—μ„œ μ‚΄νŽ΄λ³΄μž!


이 글은 μ•„λž˜ μ‚¬μ΄νŠΈλ₯Ό μ°Έκ³ ν•˜μ—¬ μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€πŸ™‚
https://jestjs.io/docs/using-matchers

profile
I'm jerry

0개의 λŒ“κΈ€