TDD

basic

테오구 2022. 4. 19. 18:05
728x90

자동 환경 설정

npm i jest -g
npm i jest
"scripts": {
    "test": "jes --watchAll"
  },

지금 내가 작업하는 것들에 대해서만 작용하고 싶다면

"scripts": {
    "test": "jest --watch"
  },

git init를하고 .gitignore 파일을 생성 후 .gitignore에

node_modules/*

이렇게하면 내가 수정한 파일만 test가 됩니다.

Unit test

const add = require('../add.js')

test('add 1 + 2 to equal', () => {
  // 테스트 코드 작성!
	// ===
  expect(add(1, 2)).toBe(3)
})

add.js에 있는 add함수를 사용하여 그 값이 3이여야 한다.

npm i @types/jest
test('null', () => {
  const n = null;
  expect(n).toBeNull(); // toBeNull matches only null
  expect(n).toBeDefined(); // toBeUndefined matches only undefined
  expect(n).not.toBeUndefined(); // toBeDefined is the opposite of toBeUndefined
  expect(n).not.toBeTruthy(); // toBeTruthy matches anything that an if statement treats as true
  expect(n).toBeFalsy(); // toBeFalsy matches anything that an if statement treats as false
});
test('two plus two', () => {
  const value = 2 + 2;
  expect(value).toBeGreaterThan(3); // 3보다 커야한다.
  expect(value).toBeGreaterThanOrEqual(3.5); // 3.5보다 크거나 같아야한다.
  expect(value).toBeLessThan(5); // 5보다 작아야한다.
  expect(value).toBeLessThanOrEqual(4.5); // 4.5보다 작거나 작아야한다.

  // toBe and toEqual are equivalent for numbers
  expect(value).toBe(4);
  expect(value).toEqual(4);
});
728x90

'TDD' 카테고리의 다른 글

FIRST 원칙  (0) 2022.04.25
좋은 테스트 코드  (0) 2022.04.23
stub  (0) 2022.04.22
mock  (0) 2022.04.19
TDD란  (0) 2022.04.19