Closed Dolphin-PC closed 3 weeks ago
테스트 설정 보일러플레이트가 적은 vitest
를 사용하여, 설정의 스트레스를 줄이고 코드를 빠르게 작성하는 것이 더 좋다고 생각했다.vite.config.ts
로 설정을 진행한다.plugins : []
로 되어있었는데, 이게 문제였던듯 하다.
import { defineConfig, InlineConfig, UserConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import svgr from "vite-plugin-svgr";
import path from "path";
interface VitestConfigExport extends UserConfig { test: InlineConfig; }
// https://vitejs.dev/config/ export default defineConfig({ plugins: [react(), svgr()], test: { globals: true, environment: "jsdom", setupFiles: "./src/test/setup.ts", }, resolve: { alias: { "@": path.resolve(__dirname, "./src"), }, }, } as VitestConfigExport);
대부분의 React컴포넌트는 Router컴포넌트 하위의 컴포넌트이기에, Router가 주입이 되지 않았다면, useNavigate와 같은 hook을 사용할 수 없어 오류가 발생했다.
이를 해결하기 위해, util 함수를 만들어 해결할 수 있다.
// test-util.tsx
import { ReactNode } from "react";
import { render } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import userEvent from "@testing-library/user-event";
export * from "@testing-library/react";
/**
@see https://testing-library.com/docs/example-react-router/#reducing-boilerplate */ export const renderWithRouter = (ui: ReactNode, { route = "/" } = {}) => { window.history.pushState({}, "Test page", route);
return { user: userEvent.setup(), ...render(ui, { wrapper: BrowserRouter }), }; };
- 이를 이용해서, 사용한다면 아래와 같이 렌더링 할떄 오류가 발생하지 않는다.
![image](https://github.com/user-attachments/assets/7c475dcf-0fef-4743-8b25-82909339d42c)
데이터 100,000개
렌더링 테스트
Windowing 적용전 (렌더링 시간 976ms)
Windowing 적용후 (렌더링 시간 16ms)