TypeScript_JEST TypeScript로 Node TestCast만들기 : 실제 테스트 진행 -2

이제 연산작용을 적용해서 실제 JEST에서 테스트를 진행해 보겠습니다.


이전글 : TypeScript_JEST TypeScript로 Node TestCast만들기 : npm 프로젝트 생성 및 샘플 테스팅 -1


// src/test/Main.test.ts
import {Fun} from '../app/Main'

describe('Main test suite', () => {
test('first test', () => {
console.log('test work!!!')
const str = Fun.toUpperC('str');
expect(str).toBe('STR'); // Fun.toUpper의 결과가 문자열 STR 이어야 한다
});
})

코드1)




"STR"을 기대했지만 실제로 ""을 받았습니다. 실제 함수의 용도를 보면 들어간 문자열을 대문자로 바꾸는 기능이라고 할수 있는데 이 테스트에서 failed가 나왔습니다. 그럼 이제 실제 함수를 수정하도록 하겠습니다.


// src/app/Main.ts
export class Fun{
public static toUpperC(arg : string){
return arg.toUpperCase();
}
}

코드2)



하지만 실제로는 코드1처럼 간단하게 작성해서 테스트 하지 않습니다. 


// src/app/Main.ts
import { parse, UrlWithParsedQuery } from "url";

export class Fun{
public static parseUrl(url : string) : UrlWithParsedQuery {
return parse(url, true);
}
public static toUpperC(arg : string){
return arg.toUpperCase();
}
}

코드3)


코드3에서 url을 파싱하는 메소드를 만들도록 하겠습니다.


// src/test/Main.test.ts
import {Fun} from '../app/Main'

describe('Main test suite', () => {
test('first test', () => {
console.log('test work!!!')
const str = Fun.toUpperC('str');
expect(str).toBe('STR'); // Fun.toUpper의 결과가 문자열 STR 이어야 한다
});

test('parse URL', () => {
const url : string = 'http://localhost:3000/signup';
const port : string = url.split('localhost:')[1].split('/')[0];
const protocol : string = url.split('//')[0];
const parseUrl = Fun.parseUrl(url);
expect(parseUrl.href).toBe(url)
expect(parseUrl.port.toString()).toBe(port)
expect(parseUrl.protocol).toBe(protocol)
})
})

코드4)




코드4를 실행하면 테스트가 통과됨을 알수 있습니다.


expect(parseUrl.query).toBe({})

코드5)






하지만 코드5를 추가하고나서 문제가 생긴것을 알수 있습니다. 이는 Object를 비교하기 때문에 생긴 문제 입니다. 원시 원소와 다르게 배열, 또는 객체들은 참조를 하기 때문에 설사 똑간은 값을 가져도 다른 메모리 주소에서 갖고오기때문에 다르다고 판단하는 것입니다.


expect(parseUrl.query).toEqual({})

코드6)




이때 코드6에서  toBe => toEqual을 사용하면 값만을 비교해서 테스트 케이스가 통과될수 있습니다.


test('parse URL with Query', () => {
const url = Fun.parseUrl('http://localhost:3000/signup?username=alex&nickname=super');
const query : object = {
username : 'alex',
nickname : 'super'
}

expect(url.query).toEqual(query)
})

코드7)



위 코드7또한 객체의 값을 넣어서 비교를 할때 테스트 케이스가 통과되는것을 알수 있습니다.


// src/test/Main.test.ts
import {Fun} from '../app/Main'

describe('Main test suite', () => {

// describe안의 테스트가 실행되기전 한번 beforeAll을 실행한다.
beforeAll(() => {
console.log('before all')
})

// 각 테스트를 실행하기 전 beforeEach을 실행한다.
beforeEach(() => {
console.log('before each')
})

test('first test', () => {
console.log('test work!!!')
const str = Fun.toUpperC('str');
expect(str).toBe('STR'); // Fun.toUpper의 결과가 문자열 STR 이어야 한다
});

test('parse URL', () => {
const url : string = 'http://localhost:3000/signup';
const port : string = url.split('localhost:')[1].split('/')[0];
const protocol : string = url.split('//')[0];
const parseUrl = Fun.parseUrl(url);
expect(parseUrl.href).toBe(url)
expect(parseUrl.port.toString()).toBe(port)
expect(parseUrl.protocol).toBe(protocol)
expect(parseUrl.query).toEqual({})
})

test('parse URL with Query', () => {
const url = Fun.parseUrl('http://localhost:3000/signup?username=alex&nickname=super');
const query : object = {
username : 'alex',
nickname : 'super'
}

expect(url.query).toEqual(query)
})
})

코드8)



코드8에서는 'beforeAll', 'beforeEach'에대한 구동 방법때문에 작성했습니다. beforeAll은 해당 테스트 안에 한번만 실행을하고 beforeEach는 매 테스트 마다 실행을 하는 것입니다. 위 사진의 console.log를 확인하면 이해하기가 쉽습니다.


// 추가 테스트 케이스 작업에 대한 메모
test.todo("추가되는 코드에 대한 테스트 케이스 작성!!!")

코드9)




마지막으로 코드9는 향후 개발할 테스트코드에 대한 메모 입니다. 테스트 결과에 영향을 주지 않지만 향후 개발자가 테스트 결과를 볼때 다음 작업에 대해서 간단하게 설명을 적습니다.


해당 gitHub : https://github.com/Alex-Choi0/node_testing_excercise1.git


댓글

이 블로그의 인기 게시물

Lesson 12_1 프로퍼티 노드(Property Node)

DAQ로 전압 측정하기-2

Lesson 12_2 참조를 이용한 프로퍼티노드(Property Node)