Jest 1 Matcher

Jest 1 - Matcher

๊ณผ์ œ์ „ํ˜•์„ ์ค€๋น„ํ•˜๋ฉด์„œ ๊ฒธ์‚ฌ๊ฒธ์‚ฌ test library์— ๋Œ€ํ•ด ๋ณต์Šตํ•˜๊ณ  ์ •๋ฆฌํ•˜๊ธฐ๋กœ ํ•œ๋‹ค
Jest์—์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋น„๊ตํ•˜๋ฉฐ ๊ฐ’์ด ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธํ• ์ˆ˜ ์žˆ๋„๋ก ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค ์ด๋ฅผ Matcher๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ Matcher๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๋Š” it, test ๋‘˜์ค‘ ํ•œ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‘๊ฐœ์˜ ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ์€ ๋™์ผํ•˜๊ณ  ๊ฐœ๋ฐœ์ž๊ฐ€ ์›ํ•˜๋Š”๋ฐ์— ์ดˆ์ ์„ ๋‘˜๊ฒฝ์šฐ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค ์˜ˆ๋ฅผ ๋“ค์–ด it should โ€ฆ ์ฒ˜๋Ÿผ ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•œ์ง€ ํ…Œ์ŠคํŠธํ• ๋•Œ it์„ ์“ฐ๊ฑฐ๋‚˜ ๋™์ž‘, ๊ธฐ๋Šฅ์— ํ…Œ์ŠคํŠธ๋ฅผ ํ• ๊ฒฝ์šฐ test๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค

test('์„ค๋ช…', () => {
  // ํ…Œ์ŠคํŠธ ์ฝ”๋“œ
});

// ๋˜๋Š”
it('์„ค๋ช…', () => {
  // ํ…Œ์ŠคํŠธ ์ฝ”๋“œ
});

Matcher


toBe

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋น„๊ตํ•จ์ˆ˜ js์—์„œ == ์—ฐ์‚ฐ์ž์™€ ๋™์ผ
test('๊ธฐ๋ณธ ๋น„๊ตํ•จ์ˆ˜ ==', () => {
  expect(1).toBe(1);
});

toEqual

์ฐธ์กฐ๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๋น„๊ต, ์ฃผ๋กœ 2๊ฐœ์˜ ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์„ ๋น„๊ต ์ „ํ˜€ ๋‹ค๋ฅธ ๊ฐ์ฒด ๋ฐ ์ธ์Šคํ„ด์Šค๋ผ๋„ ๊ฐ’์œผ๋กœ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™๋‹ค๊ณ  ์ธ์‹
test('๋น„๊ตํ•จ์ˆ˜', () => {
  const data = { one: 1 };
  data['two'] = 2;
  expect(data).toEqual({ one: 1, two: 2 });

  // js๋Š” ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ• ๋•Œ ๊ฐ’์ด ์•„๋‹Œ ์ฐธ์กฐ๋กœ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅด๋‹ค๊ณ  ๋‚˜์˜ด => ์ฝ˜์†”์—์„œ๋Š” false๋กœ ๋‚˜์˜จ๋‹ค
  console.log(data === { one: 1, two: 2 });

  // ์•„๋ž˜๋„ false๋กœ ๋‚˜์˜จ๋‹ค
  test('๊ธฐ๋ณธ ๋น„๊ตํ•จ์ˆ˜ ===', () => {
  expect({ one: 1, two: 2 }).toBe({ one: 1, two: 2 });
});
});

toStrictEqual

๊ฐ์ฒด ๋‚ด์˜ ๊ฐ’์˜ ๊นŠ์€ ๋น„๊ต๋ฅผ ์ง„ํ–‰ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์„ ๊ฒฝ์šฐ false๋ฅผ return
  • ๊ฐ์ฒด ๋‚ด์˜ undefined ์†์„ฑ์ด ์žˆ๊ฑฐ๋‚˜ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ
  • ๊ฐ์ฒด์˜ ์†์„ฑ์— undefined๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ
  • ๊ฐ์ฒด์˜ ์†์„ฑ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅผ ๋•Œ
    test('๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ์†์„ฑ์˜ ์ •ํ™•์„ฑ๊นŒ์ง€ ๊ฒ€์‚ฌ', () => {
    const data = { one: 1, three: undefined };
    data['two'] = 2;
    expect(data).toStrictEqual({ one: 1, two: 2 });
    });
    

toBeDefined

๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด undefined์ธ์ง€ ๊ฒ€์‚ฌ
test('๋ณ€์ˆ˜์˜ ๊ฐ’์ด undefined ์ธ์ง€ ํ™•์ธ', () => {
  let test = undefined;
  expect(test).toBeDefined();
});

toBeTruthy / toBeFalsy

false, true ๊ฐ’ ๊ฒ€์‚ฌ
test('bool๊ฐ’ ํ™•์ธ', () => {
  expect(true).toBeTruthy();
  expect(false).toBeFalsy();
});

toMatch

์ •๊ทœ ํ‘œํ˜„์‹ ๊ฒ€์‚ฌ
test('์ •๊ทœ ํ‘œํ˜„์‹', () => {
  expect('test@naver.com').toMatch(/.naver.com/);
});

toHaveProperty

key, value๊ฐ’ ๊ฒ€์‚ฌ
test('๊ฐ์ฒด์˜ ํ‚ค, ๊ฐ’ ๊ฒ€์‚ฌ', () => {
  const test = {
    test1: 1,
    test2: 2,
  };

  expect(test).toHaveProperty('test1', 1);
});

toHaveLength, toContain

๋ฐฐ์—ด ๊ธธ์ด, ๋ฐฐ์—ด ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
test('๋ฐฐ์—ด ๊ด€๋ จ', () => {
  const animals = ['lion', 'cat', 'dog', 'elephant', 'giraffe'];
  expect(animals).toHaveLength(5);
  expect(animals).toContain('dog');
});

toThrow

throw๊ฐ€ ์‹คํ–‰ ๋๋Š”์ง€ ๊ฒ€์‚ฌ
function compileAndroidCode(os) {
  if(os !== 'android')
  throw new Error('you are using the wrong os');
}

test('์˜ˆ์™ธ ์ฒ˜๋ฆฌ', () => {
  // param์˜ ๊ฐ’๊ณผ error ๋ฉ”์„ธ์ง€๊ฐ€ ์ผ์น˜ํ•˜๋Š” ์ง€๋„ ์ฒดํฌ
  expect(() => compileAndroidCode('ios')).toThrow('you are using the wrong os');
});