๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
TestCode

[Jest] Test ์ž‘์„ฑ - Matcher (toBe(), not.toBe(), toEqual(), toStrictEqual(), toBeNull(),toBeUndefined(),toBeDefined(),toBeTruthy(),toBeFalsy())

by DevIseo 2023. 2. 2.

 ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ

์ฝ”๋“œ1. toBe()

//fn.js
const fn = {
  add : (num1,num2) => num1+num2
}

module.exports = fn
//fn.test.js
const fn = require('./fn')
test('1์€ 1์ด์•ผ',()=>{
  expect(1).toBe(1)
})

test('2๋”ํ•˜๊ธฐ 3์€ 5์•ผ',()=>{
  expect(fn.add(2,3)).toBe(5)
})
test('3๋”ํ•˜๊ธฐ 3์€ 5์•ผ',()=>{
  expect(fn.add(3,3)).toBe(5)
})

๊ฒฐ๊ณผ1.


์ฝ”๋“œ2. toBe(), not.toBe()

const fn = require('./fn')
test('1์€ 1์ด์•ผ',()=>{
  expect(1).toBe(1)
})

test('2๋”ํ•˜๊ธฐ 3์€ 5์•ผ',()=>{
  expect(fn.add(2,3)).toBe(5)
})
test('3๋”ํ•˜๊ธฐ 3์€ 5๊ฐ€ ์•„๋‹ˆ์•ผ',()=>{
  expect(fn.add(3,3)).not.toBe(5)
})

๊ฒฐ๊ณผ2


์ฝ”๋“œ3. toBe(), toEqual()

//fn.js
const fn = {
  add : (num1,num2) => num1+num2,
  makeUser:(name,age)=>({name,age})
}

module.exports = fn
//fn.test.js
const fn = require('./fn')
test('์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜', ()=>{
  expect(fn.makeUser('iseo',27)).toBe({
    name:'iseo',
    age:27,
  })
})
test('์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜', ()=>{
  expect(fn.makeUser('iseo',27)).toEqual({
    name:'iseo',
    age:27,
  })
})

๊ฒฐ๊ณผ3


์ฝ”๋“œ4. toEqual(), toStrictEqual()

const fn = require('./fn')

test('์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜', ()=>{
  expect(fn.makeUser('iseo',27)).toEqual({
    name:'iseo',
    age:27,
  })
})

test('์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜', ()=>{
  expect(fn.makeUser('iseo',27)).toStrictEqual({
    name:'iseo',
    age:27,
  })
})

๊ฒฐ๊ณผ4


์ฝ”๋“œ5. ์ธ์ž ์ถ”๊ฐ€

//fn.js
const fn = {
  add : (num1,num2) => num1+num2,
  makeUser:(name,age,gender)=>({name,age,gender:undefined})
}

module.exports = fn
//fn.test.js
const fn = require('./fn')

test('์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜', ()=>{
  expect(fn.makeUser('iseo',27)).toEqual({
    name:'iseo',
    age:27,
  })
})

test('์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜', ()=>{
  expect(fn.makeUser('iseo',27)).toStrictEqual({
    name:'iseo',
    age:27,
  })
})

 


์ฝ”๋“œ6. toBeNull(),toBeUndefined(),toBeDefined(),toBeTruthy(),toBeFalsy()

//fn.test.js
const fn = require('./fn')

//toBeNull
//toBeUndefined
//toBeDefined

test('null์€ null์ž…๋‹ˆ๋‹ค',()=>{
  expect(null).toBeNull()
})

//toBeTruthy
//toBeFalsy
test('0์€ false์ž…๋‹ˆ๋‹ค',()=>{
  expect(fn.add(1,-1)).toBeFalsy()
})

๊ฒฐ๊ณผ6


์ฝ”๋“œ7

const fn = require('./fn')

//toBeNull
//toBeUndefined
//toBeDefined

test('null์€ null์ž…๋‹ˆ๋‹ค',()=>{
  expect(null).toBeNull()
})

//toBeTruthy
//toBeFalsy
test('๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฌธ์ž์—ด์€ true',()=>{
  expect(fn.add('Decision To','Leave')).toBeTruthy()
})

๊ฒฐ๊ณผ7

 

๋Œ“๊ธ€