Skip to content

Commit

Permalink
[Feat] 테스트 코드 초기 세팅 (#439)
Browse files Browse the repository at this point in the history
* build: library 설치

* feat: lint 설정

* feat: config 설정

* feat: custom render 생성

* build: library 추가 설치

* feat: test setup 파일 추가

* chore: alias 추가

* fix: custon render option 속성 optional 하게 받기

* chore: 주석 제거

* build: test library 버전업

* fix: custom render type 수정

* feat: custom render에 memory router 추가

* feat: custom render에 query client provider 추가

* fix: fillStyle을 null로 설정할 수 없는 에러 해결

* fix: test 환경에 modal div 생성

* fix: custom render에 form provider 추가

* fix: window 함수 선언

* docs: README 수정
  • Loading branch information
eonseok-jeon authored Sep 3, 2024
1 parent 0a48579 commit 83f9bf2
Show file tree
Hide file tree
Showing 8 changed files with 906 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
'plugin:prettier/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
'plugin:testing-library/react',
'plugin:vitest/recommended',
'plugin:jest-dom/recommended',
],
rules: {
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
<img src="https://img.shields.io/badge/Tanstack%20Query-FF4154?style=flat-square&logo=react-query&logoColor=white" alt="Tanstack Query Badge"/></a>
<img src="https://img.shields.io/badge/React--hook--form-EC5990?style=flat-square&logo=react-hook-form&logoColor=white" alt="React-hook-form Badge"/></a>
<img src="https://img.shields.io/badge/Date--fns-3D7DCA?style=flat-square&logo=date-fns&logoColor=white" alt="Date-fns Badge"/></a>
<img src="https://img.shields.io/badge/Lottie--react-8DD6F9?style=flat-square&logo=lottie-react&logoColor=white" alt="Lottie-react Badge"/></a>
<img src="https://img.shields.io/badge/Lottie--web-4F8DF5?style=flat-square&logo=lottie&logoColor=white" alt="Lottie-web Badge"/></a>
<img src="https://img.shields.io/badge/Cloudflare-F38020?style=flat-square&logo=cloudflare&logoColor=white" alt="Cloudflare Badge"/></a>
<img src="https://img.shields.io/badge/Firebase-FFCA28?style=flat-square&logo=firebase&logoColor=white" alt="Firebase Badge"/></a>
<img src="https://img.shields.io/badge/Amplitude-FF9D00?style=flat-square&logo=amplitude&logoColor=white" alt="Amplitude Badge"/></a>
<img src="https://img.shields.io/badge/Testing%20Library-E33332?style=flat-square&logo=testing-library&logoColor=white" alt="Testing Library Badge"/>
<img src="https://img.shields.io/badge/Vitest-6E9F18?style=flat-square&logo=vitest&logoColor=white" alt="Vitest Badge"/>

## 만든 사람들

Expand All @@ -39,7 +41,7 @@
</td>
</tr>
<tr align="center">
<td><sub>24.02 ~ 24.07</sup></td>
<td><sub>24.02 ~ 현재</sup></td>
<td>
전언석 <br />
</td>
Expand Down
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"test": "vitest"
},
"dependencies": {
"@amplitude/analytics-browser": "^2.9.3",
Expand All @@ -34,8 +35,12 @@
"devDependencies": {
"@hookform/devtools": "^4.3.1",
"@tanstack/eslint-plugin-query": "^5.35.6",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vanilla-extract/vite-plugin": "^4.0.9",
Expand All @@ -44,13 +49,18 @@
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest-dom": "^5.4.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"eslint-plugin-testing-library": "^6.3.0",
"eslint-plugin-vitest": "^0.5.4",
"jsdom": "^25.0.0",
"prettier": "^3.2.5",
"rollup-plugin-visualizer": "^5.12.0",
"typescript": "^5.2.2",
"vite": "^5.2.0"
"vite": "^5.2.0",
"vitest": "^2.0.5"
}
}
18 changes: 18 additions & 0 deletions src/tests/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import '@testing-library/jest-dom/vitest';

/* @ts-ignore */
HTMLCanvasElement.prototype.getContext = () => {
return {
fillStyle: '',
fillRect: vitest.fn(),
};
};

beforeAll(() => {
const portalEl = document.createElement('div');
portalEl.id = 'modal';
document.body.appendChild(portalEl);
});

// beforeEach(() => {});
// afterAll(() => {});
58 changes: 58 additions & 0 deletions src/tests/test-utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, type RenderOptions } from '@testing-library/react';
import DeviceTypeProvider from 'contexts/DeviceTypeProvider';
import RecruitingInfoProvider from 'contexts/RecruitingInfoProvider';
import ThemeProvider from 'contexts/ThemeProvider';
import type { ReactElement, ReactNode } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { MemoryRouter, type MemoryRouterProps } from 'react-router-dom';

interface AllTheProvidersProps extends MemoryRouterProps {
children: ReactNode;
}

export const AllTheProviders = ({ children, ...memoryRouterProps }: AllTheProvidersProps) => {
window.alert = vi.fn();
window.scrollTo = vi.fn();

const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
gcTime: 1000 * 60 * 60,
retry: false,
},
},
});

const method = useForm({ mode: 'onBlur' });

return (
<QueryClientProvider client={queryClient}>
<FormProvider {...method}>
<MemoryRouter {...memoryRouterProps}>
<ThemeProvider>
<DeviceTypeProvider>
<RecruitingInfoProvider>{children}</RecruitingInfoProvider>
</DeviceTypeProvider>
</ThemeProvider>
</MemoryRouter>
</FormProvider>
</QueryClientProvider>
);
};

const customRender = (
ui: ReactElement,
options?: Omit<RenderOptions, 'wrapper'>,
memoryRouterProps?: MemoryRouterProps,
) =>
render(ui, {
wrapper: ({ children }) => <AllTheProviders {...memoryRouterProps}>{children}</AllTheProviders>,
...options,
});

export * from '@testing-library/react';
export { customRender as render };
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"noEmit": true,
"jsx": "react-jsx",

/* testing library */
"types": ["vitest/globals", "@testing-library/jest-dom"],

/* Linting */
"strict": true,
"noUnusedLocals": true,
Expand All @@ -33,9 +36,10 @@
"@utils/*": ["common/utils/*"],
"contexts/*": ["contexts/*"],
"styles/*": ["styles/*"],
"tests/*": ["tests/*"],
"views/*": ["views/*"]
}
},
"include": ["src"],
"include": ["src", "./src/tests/setupTests.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
7 changes: 7 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="vitest/config" />
import path from 'path';

import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
Expand Down Expand Up @@ -55,7 +56,13 @@ export default defineConfig({
},
{ find: 'contexts', replacement: path.resolve(__dirname, 'src/contexts') },
{ find: 'styles', replacement: path.resolve(__dirname, 'src/styles') },
{ find: 'tests', replacement: path.resolve(__dirname, 'src/tests') },
{ find: 'views', replacement: path.resolve(__dirname, 'src/views') },
],
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/tests/setupTests.ts',
},
});
Loading

0 comments on commit 83f9bf2

Please sign in to comment.