<aside> 💻
이 브랜치에서는 React 컴포넌트에 대한 효율적인 테스트 구현을 위해 다음과 같은 계획을 수립했습니다:
Jest 및 관련 패키지 설치
npm install -D jest @types/jest @testing-library/react @testing-library/jest-dom jest-environment-jsdom
TypeScript 설정 파일 구성
Jest 환경 설정 파일 생성
src/
├── components/
│ ├── auth/
│ │ └── __tests__/
│ │ └── LoginForm.test.tsx
│ └── auth/
│ └── __tests__/
│ └── RegisterForm.test.tsx
├── pages/
│ ├── __tests__/
│ │ └── TodoListPage.test.tsx
</aside>
npm install -D jest @types/jest @testing-library/react @testing-library/jest-dom jest-environment-jsdom
{
"extends": "./tsconfig.app.json",
"compilerOptions": {
"types": ["node", "jest", "@testing-library/jest-dom"],
"module": "CommonJS",
"moduleResolution": "Node",
"esModuleInterop": true,
"allowJs": true,
"jsx": "react-jsx"
},
"include": ["src", "**/*.test.ts", "**/*.test.tsx", "jest.setup.ts"]
}
/** @type {import('jest').Config} */
module.exports = {
preset: "ts-jest",
testEnvironment: "jest-environment-jsdom",
transform: {
"^.+\\\\.(ts|tsx)$": ["ts-jest", {
tsconfig: "tsconfig.test.json"
}]
},
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
"\\\\.(css|less|sass|scss)$": "identity-obj-proxy"
},
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testEnvironmentOptions: {
customExportConditions: ['']
}
};
{
"scripts": {
"test": "jest",
"test:watch": "jest --watch"
}
}
/// <reference types="@testing-library/jest-dom" />
import '@testing-library/jest-dom';
import { TextEncoder, TextDecoder } from 'util';
declare global {
interface JestMatchers<R> {
toBeInTheDocument(): R;
toHaveClass(className: string): R;
}
}
if (!globalThis.TextEncoder) {
Object.defineProperty(globalThis, 'TextEncoder', {
value: TextEncoder,
writable: true,
configurable: true,
});
Object.defineProperty(globalThis, 'TextDecoder', {
value: TextDecoder,
writable: true,
configurable: true,
});
}
export {};
이렇게 기본 설정을 하고, 첫 테스트 파일을 작성합니다.
// src/pages/__tests__/TodoListPage.test.tsx
describe("TodoListPage", () => {
it("renders todo list correctly", async () => {
renderWithProviders(<TodoListPage />);
// 로딩 상태 확인
expect(screen.getByText(/loading/i)).toBeInTheDocument();
// 데이터 로드 후 상태 확인
await waitFor(() => {
expect(screen.getByText("Test Todo 1")).toBeInTheDocument();
expect(screen.getByText("Test Todo 2")).toBeInTheDocument();
});
});
// Todo 완료 상태 테스트
it("displays completed status correctly", async () => {
renderWithProviders(<TodoListPage />);
await waitFor(() => {
const completedTodo = screen.getByText(/Test Todo 2/);
expect(completedTodo).toHaveClass("line-through");
});
});
});
테스트 포인트: