프로젝트를 구성할 root 디렉터리에서 아래 cmd 실행
$> npm init -y
$> npm i -D typescript
$> touch tsconfig.json
src 디렉터리를 만들고, 그 하위에 간단한 ts 코드 작성
const hello = () => 'Hello, Peter!';
(index.ts)
tsconfig.json 파일 설정
{
"include": ["src"],
"compilerOptions": {
"outDir": "build",
"target": "es6",
"esModuleInterop": true,
"lib": ["ES6"],
"module": "CommonJS"
}
}
import * as crypto from 'crypto';
const crypto = require('crypto');
// esModuleInterop: true 이면 아래와 같이 간단하게 import 가능
// 공식문서 설명: Allow 'import x from y' when a module doesn't have a default export.
// crypto module 의 경우 esModuleInterop 옵션을 별도로 주지 않고 아래와 같이 작성하면 Module '"crypto"' has no default export. 오류 발생
import crypto from 'crypto';
package.json 파일 수정
{
"name": "typechain",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^4.8.4"
}
}
빌드(트랜스파일) 실행
$> npm run build
test 환경 구축을 위해 jest 를 설치합니다.
$> npm i jest @types/jest ts-jest -D
jest.config.js 파일을 프로젝트 root 폴더에 생성하고 아래와 같이 설정합니다.
module.exports = {
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
}
이제 src 폴더 하위에 *.spec.ts 파일을 생성하여 테스트 코드를 작성한뒤, 프로젝트 root 폴더 CLI 에서 jest 를 실행하면 테스트 코드가 실행되는 것을 확인할 수 있습니다.
$> npm i -D ts-node
$> npm i nodemon
"scripts": {
"start:dev": "nodemon --exec ts-node src/index.ts",
},