์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- ํ๋ก ํธ์๋
- jest
- ์ฝ๋ฉ๋ ํ
- react
- javascript
- queryprovider
- canvas
- ์น ๋ธ๋ผ์ฐ์ ๊ตฌ์กฐ
- ์ํ์ฝ๋ฉ
- tanstack-query
- ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ
- ์ฝ๋ฉ์ผ๊ธฐ
- typescript
- canvas image
- JAVA Script
- clone coding
- canvas js
- JS
- negative margin
- ๋ฐ๋๋ผ JS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ
- tailwindcss
- HTML ํ์ผ๊ตฌ์กฐ
- axios-mock-adapte
- ๋ ธ๋ง๋ ์ฝ๋
- HTML
- ๋ ธ๋ง๋์ฝ๋
- ์น ๋ธ๋ผ์ฐ์ ๋ ๋๋ง
- ์ฝ์ฝ์ํก ํด๋ก ์ฝ๋ฉ
- next.js
- CSS
- Today
- Total
Coding Archive
[next.js & jest] Jest ์ค์น ๋ฐ ๊ตฌ์ฑ ๊ฐ์ด๋: Next.js + TypeScript ํ๊ฒฝ์์์ Jest ์ฌ์ฉ๋ฒ ๋ณธ๋ฌธ
[next.js & jest] Jest ์ค์น ๋ฐ ๊ตฌ์ฑ ๊ฐ์ด๋: Next.js + TypeScript ํ๊ฒฝ์์์ Jest ์ฌ์ฉ๋ฒ
์ฝ๋ฑ์ด 2024. 10. 22. 22:09

Jest ์ค์น ๋ฐ ๊ตฌ์ฑ ๊ฐ์ด๋: Next.js + TypeScript ํ๊ฒฝ์์์ Jest ์ฌ์ฉ๋ฒ
์ด๋ฒ์๋ Next.js์ TypeScript ํ๋ก์ ํธ์์ Jest๋ฅผ ์ค์นํ๊ณ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ๋จ๊ณ๋ณ๋ก ์ค๋ช ํฉ๋๋ค. Jest๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ ์คํธ ํ๋ ์์ํฌ๋ก, ํ ์คํธ ์ฝ๋ ์์ฑ๊ณผ ํ ์คํธ ์คํ์ ๋น ๋ฅด๊ณ ์ฝ๊ฒ ํ ์ ์๋๋ก ๋์์ค๋๋ค.
1. Jest ์ค์น
Step 1: Jest ํจํค์ง ์ค์น
๋จผ์ , Jest์ TypeScript ํ๊ฒฝ์์ Jest๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ํ์ํ ํจํค์ง๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค.
yarn add jest ts-jest jest-environment-jsdom @types/jest @testing-library/react @testing-library/jest-dom @testing-library/dom --dev
- jest: Jest์ ํต์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ts-jest: TypeScript๋ฅผ ์ฌ์ฉํ์ฌ Jest ํ ์คํธ๋ฅผ ์คํํ ์ ์๋๋ก ๋์์ฃผ๋ ํจํค์ง.
- @types/jest: Jest์ ํ์ ์ ์ ํ์ผ๋ก, TypeScript ํ๋ก์ ํธ์์ Jest์ ํ์ ์ ์ธ์ํ ์ ์๊ฒ ํด์ค.
- jest-environment-jsdom: DOM ํ๊ฒฝ์์ Jest ํ ์คํธ๋ฅผ ์คํํ ์ ์๊ฒ ํด์ฃผ๋ ํจํค์ง.
- @types/jest: Jest์ ํ์ ์ ์ ํ์ผ๋ก, TypeScript์์ Jest๋ฅผ ์ฌ์ฉํ ๋ ํ์ ์ง์์ ์ ๊ณต.
- @testing-library/react: React ์ปดํฌ๋ํธ์ UI๋ฅผ ํ ์คํธํ๋ ๋ฐ ์ฌ์ฉ. ์ฌ์ฉ์ ๊ด์ ์์ UI๋ฅผ ํ ์คํธํ ์ ์๊ฒ ๋์์ค.
- @testing-library/jest-dom: Jest์์ DOM ๊ด๋ จ ๋งค์ฒ(์: toBeInTheDocument() ๋ฑ)๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋์์ค.
- @testing-library/dom: DOM ์กฐ์์ ํ ์คํธํ ์ ์๋ ๋๊ตฌ๋ฅผ ์ ๊ณต.
2. Jest ๊ธฐ๋ณธ ์ค์
Step 2: Jest ์ค์ ํ์ผ ์์ฑ
Next.js ํ๋ก์ ํธ์์ Jest๋ฅผ ์ค์ ํ๋ ค๋ฉด jest.config.js ํ์ผ๊ณผ jest.setup.js ํ์ผ์ ์์ฑํด์ผ ํฉ๋๋ค.
/* jest.setup.js */
import '@testing-library/jest-dom' // React Testing Library์์ ์ฌ์ฉํ๋ ์ค์
/* jest.config.cjs */
const nextJest = require('next/jest'); // Next.js ํ๋ก์ ํธ์์ Jest๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ฃผ๋ ํจ์
const createJestConfig = nextJest({
dir: './', // Next.js ์ฑ์ ๋ฃจํธ ๋๋ ํ ๋ฆฌ ์ค์
});
const customJestConfig = {
testEnvironment: 'jest-environment-jsdom', // DOM ํ๊ฒฝ์์ ํ
์คํธ
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], // ํ
์คํธ ํ๊ฒฝ ์ค์ ํ์ผ
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1', // ์ ๋ ๊ฒฝ๋ก๋ฅผ ์ค์ ํ๊ธฐ ์ํ ๊ฒฝ๋ก ๋งคํ
},
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest', // TypeScript ํ์ผ์ Jest๊ฐ ์ธ์ํ๋๋ก ์ค์
},
};
module.exports = createJestConfig(customJestConfig);โ
3. ํด๋ ๊ตฌ์กฐ
ํ๋ก์ ํธ ๋ด์ ํ ์คํธ ํ์ผ๋ค์ ์ผ๋ฐ์ ์ผ๋ก __tests__ ๋๋ ํ ๋ฆฌ ์์ .test.tsx ๋๋ .spec.ts ํ์ผ ํ์์ผ๋ก ์์ฑํฉ๋๋ค.
/project-root
โโโ /src
โ โโโ /app
โ โ โโโ login.tsx
โ โโโ /components
โ โ โโโ Header.tsx
โ โโโ /__tests__
โ โ โโโ login.test.tsx
โโโ jest.config.js
โโโ jest.setup.js
โโโ tsconfig.json
โโโ package.json
- __tests__: ํ ์คํธ ํ์ผ๋ค์ ๋ชจ์ ๋๋ ํด๋์ ๋๋ค. ์ด ํด๋์ *.test.tsx ๋๋ *.spec.ts ํ์ผ๋ค์ ์ ์ฅํ ์ ์์ต๋๋ค.
- ํ
์คํธ ํ์ผ์ ํ๋ก๋์
ํ์ผ๊ณผ ๋์ผํ ๊ฒฝ๋ก์ ์์นํ ์๋ ์๊ณ , ๋ณ๋์ __tests__ ํด๋์ ์ ์ฅํ ์๋ ์์ต๋๋ค.
4. TypeScript ์ค์ ๊ณผ Jest ํตํฉ
Step 3: TypeScript ์ค์ ํ์ผ(tsconfig.json) ์ ๋ฐ์ดํธ
Jest์ TypeScript์ ํธํ์ฑ์ ์ํด tsconfig.json ํ์ผ์ Jest์ ๊ด๋ จ๋ ์ค์ ์ ์ถ๊ฐํฉ๋๋ค.
{
"compilerOptions": {
"types": ["node", "jest", "@testing-library/jest-dom"],
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "bundler",
"jsx": "preserve",
"skipLibCheck": true,
"esModuleInterop": true
...
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "__tests__/**/*.ts", "__tests__/**/*.tsx"],
"exclude": ["node_modules"]
}
- types: Jest์ Node.js์ ํ์ ์ ํฌํจํ์ฌ Jest ํ๊ฒฝ์์ TypeScript ํ์ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ํฉ๋๋ค.
- include: Jest ํ
์คํธ ํ์ผ(__tests__ ํด๋๋ *.test.sx, *.test.tsx ํ์ผ)์ ํฌํจ์ํต๋๋ค.
5. Jest ์คํ
Step 4: ํ ์คํธ ์คํ
์ค์ ์ด ์๋ฃ๋์์ผ๋ฉด package.json์ ์๋์ ๊ฐ์ด Jest ์คํ ์คํฌ๋ฆฝํธ๋ฅผ ์ถ๊ฐํฉ๋๋ค.
{
"scripts": {
...
"test": "jest",
"test:watch": "jest --watch" // ํ์ผ์ด ๋ณ๊ฒฝ๋๋ฉด ํ
์คํธ๋ฅผ ๋ค์ ์คํ
...
}
}
์ดํ ํ ์คํธ๋ฅผ ์คํํ ๋๋ ๋ค์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํฉ๋๋ค.
yarn test