라벨이 toEqual인 게시물 표시

TypeScript_JEST TypeScript로 Node TestCast만들기 : 커버리지(Coverage) 생성하고 사용하기 -4

이미지
  // jest.config.ts import type { Config } from '@jest/types' const config : Config . InitialOptions = { roots : [ '<rootDir>/src' ], transform : { '^.+ \\ .tsx?$' : 'ts-jest' }, testRegex : '(/__test__/.*|( \\ .|/)(test|spec)) \\ .[jt]sx?$' , moduleFileExtensions : [ 'ts' , 'tsx' , 'js' , 'jsx' , 'json' , 'node' ], verbose : true , collectCoverage : true , // 커버리지 사용 collectCoverageFrom : [ '<rootDir>/src/app/**/*.ts' ] // rootDir(루트 경로)에서 src/app폴더안 모든(**)폴더, 파일 및 모든 TypeScript 파일*.ts } export default config ; 커버리지를 사용한다고 하면 위 사진과 같이 테스트 결과가 다르게 나오는 것을 알수 있습니다.  HTML문서로 해당 테스트의 커버리지를 확인을 할수 있습니다.

TypeScript_JEST TypeScript로 Node TestCast만들기 : 에러상황을 테스트하기 -3

이미지
  // src/app/Main.ts import { parse , UrlWithParsedQuery } from "url" ; export class Fun { public static parseUrl ( url : string ) : UrlWithParsedQuery { if (! url || url . length <= 0 ) throw Error ( 'url is empty' ) // url이 비어있을시 에러발생 return parse ( url , true ); } public static toUpperC ( arg : string ){ return arg . toUpperCase (); } } // src/test/Main.test.ts import { Fun } from '../app/Main' describe ( 'Main test suite' , () => { // 해당 describe테스트에서 아래만 제외한다. 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:...

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 { ...