백엔드

TDD + node.js + typescript 세팅 반법

테오구 2022. 6. 12. 22:22
728x90
yarn add jest ts-jest @types/jest supertest dotenv
yarn add --dev babel-jest @babel/core @babel/preset-env

jest.config.js 파일을 프로젝트 최상단에 만들어주고 다음과 같은 내용을 작성하여 jest가 ts-jest를 사용하도록 알려주자

jest.config.js

module.exports = {
  preset: 'ts-jest', //trypeScript파일은 ts-jest에서 CommonJS구문으로 변환
  //   testEnvironment: 'node',
  setupFiles: ['dotenv/config.js'], //테스트 환경
  //   testMatch: ['**/*.spec.[jt]s?(x)', '**/*.test.[jt]s?(x)'], //테스트 파일 위치
  moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'json'],
  transform: {
    '^.+\\\\.(js|jsx)?$': 'babel-jest',
  },
  testEnvironment: 'node',
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
  },
  testMatch: [
    '<rootDir>/**/*.test.(js|jsx|ts|tsx)',
    '<rootDir>/(tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx))',
  ],
  transformIgnorePatterns: ['<rootDir>/node_modules/'],
}

babel.config.js

module.exports = {
  presets: ['@babel/preset-env'],
}

package.json

"scripts": {
  "test": "jest --setupFiles dotenv/config --forceExit --detectOpenHandles"
}
728x90