-
[Node.js] Jest를 이용한 테스트 코드 적용(2) Matcher정리node.js(노드) 2021. 4. 19. 11:05
Matcher 종류
Jest에서 사용되는 Matcher 종류
toBe()
숫자나 문자와 같은 객체가 아닌 기본형값을 비교할 때 사용됩니다.
let indexA = 10; let indexB = 20; test("tobe", () => { expect(1).toBe(1) expect(2).toBe(2) expect("jest").toBe("jest") expect(false).toBe(false) expect(indexA+10).toBe(indexB) })
toMatch()
정규식 기반의 테스트
문자열을 비교할경우 toBe()는 정확하게 일치하는지 체크하지만 toMatch()는 정규식을 이용해서 일부만 일치하는지 알 수 있다.
test("toMatch()", () => { expect("1234567890").toBe("1234567890") expect("1234567890").toMatch(/.*7890$/) //7890으로 끝나는지 체크 expect("1234567890").toMatch(/12345.*$/) //12345 로 시작하는지 체크 expect("1234567890").toMatch(/.*34567.*$/) //34567 을 포함하는지 체크 })
not
단독으로 Matcher는 아니지만 Matcher 앞에 not을 붙여줌으로써 해당 값이 아닌경우 참으로 처리함
let indexA = 10; let indexB = 20; test("tobe", () => { expect(indexA).not.toBe(indexB) //indexA가 indexB가 아닌경우 expect("1234567890").not.toMatch(/.*789$/) //789로 끝나지 않는지 체크 })
toEqual()
객체값을 비교할때 사용됩니다
//A객체 생성 let indexA = { num : 1, id : "jest" }; //B객체 생성 let indexB ={}; indexB.num = 1; indexB.id = "jest" test("tobe", () => { //임시 객체 비교 expect({ num : 1, id : "jest" }).toEqual({ num : 1, id : "jest" }) //생성한 A,B 객체 비교 expect(indexA).toEqual(indexB) })
toBeTruthy(), toBeFalsy()
해당 값이 true인지 false인지 구분
여기서 true,false는 boolean의 true,false뿐만 아니라 자바스크립트의 true,false값입니다. ex) null,0,1,""
test("toBeFalsy,toBeTruthy", () => { expect(0).toBeFalsy() //False인 경우 expect(false).toBeFalsy() //False인 경우 expect(null).toBeFalsy() //False인 경우 expect(true).toBeTruthy() //True인 경우 expect(1).toBeTruthy() //True인 경우 expect("jest").toBeTruthy() //True인 경우 })
toHaveLength(), toContain()
배열 관련 테스트
toHaveLength() 배열의 길이를 체크할 때 쓰이고, toContain() 특정 원소가 배열에 들어있는지를 테스트할 때 쓰입니다.
test("toHaveLength(), toContain()", () => { const index = ["a", "b", "c"] expect(index).toHaveLength(3) //배열의 길이 expect(index).toContain("a") //배열 안에 a 가 있느냐 expect(index).not.toContain("d") //배열 안에 d가 없느냐(not) })
beforeEach(),afterEach()
각 테스트를 실행 후 작동하는 Matcher
test()가 작동 후 실행된다.
var index = "jest" function JestToTest(){ //index를 "test"로 변환하는 함수 index = "test" return index; } afterEach(() => { //test가 끝나고 index를 "jest"로 변환 index = "jest"; }) console.log("index : " +index); //index : jest JestToTest() //index를 "test"로 변환 test("checkTest", () => { expect(index).toBe("test") console.log("index : " +index); //index : test }) test("checkJest", () => { expect(index).toBe("jest") console.log("index : " +index); //index : jest })
beforeAll(), afterAll()
각각 맨 처음과 맨 끝에 딱 한번씩만 호출됩니다. 대부분 connection과 관련이 있습니다.
let connection beforeAll(() => { conection = openConnection({ host: "...", port: "..." }) }) afterAll(() => { conection.close() })
describe(), it()
여러 개의 테스트 함수를 묶는 것이 가능합니다.
describe("group 1", () => { test("test 1-1", () => { // ... }) test("test 1-2", () => { // ... }) }) describe("group 2", () => { it("test 2-1", () => { // ... }) it("test 2-2", () => { // ... }) })
'node.js(노드)' 카테고리의 다른 글
[Node.js] Jest를 이용한 테스트 코드 적용 (1) 설치 및 사용법 (0) 2021.04.19 [node.js] 노드 js 교과서 정리 (2) req, res 객체 (0) 2020.10.28 [node.js] 노드 js 교과서 정리 (1) 핵심 개념 (0) 2020.09.25 [Node.js] Web push 정리 (1) Service Worker (0) 2020.09.12 [Node.js] NULL, undefined가 나왔을때 의심해볼만한곳 정리(계속 업데이트) (0) 2020.08.18