๐ชTEST Code
jest
๐ถ ๊ฐ๋ฐ๊ฐ๋ฐ ๐พ
2025. 2. 3. 23:13
ํ ์คํธ ํ์์ฑ
- ์๋ฌ๊ฐ ๋ ๋๋ ๊ฒฝ์ฐ → ํ๊ท ํ ์คํธ
- ์ ์ง๋ณด์๋ฅผ ํด์ผํ๋ ๊ฒฝ์ฐ
- ๋ณต์กํ ์ฝ๋๋ฅผ ๋ง์ด ๋ฐ๊ฟ์ผ ํ๋ ๊ฒฝ์ฐ
- import ๋ฅผ ๋ง์ด ํ๋ ์ํฅ๋๊ฐ ๋์ ์ฝ๋
โญ ํ ์คํธ๋ ํํํ ์ถ๊ฐํ๊ณ , ์ ๋ง ๋์์ด ๋๋ ํ ์คํธ๋ฅผ ํ์. ๋น์ฐํ๊ฑฐ๋ ํ ์คํธ ํ์ง ๋ง์ โญ
ํ ์คํธ ์ข ๋ฅ
- ์ ๋: ํจ์๋ฅผ ํ ์คํธ ํจ
- ํตํฉ
- E2E
์ค์น
npm i jest -D
npm i ts-jest @types/jest -D
npm i babel-jest @babel/core
npm i cross-env // ์๋์ฐ ํธํ์ฉ ํจํค์ง
ํ ์คํธ ์์ ๊ด๋ จ ๋ช ๋ น์ด
npx ts-jest config:init
npx cross-env NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" npx jest
Visual Studio Code Jest Test Setting
1) settings.json
{
"jest.pathToJest": "**/node_modules/.bin/jest",
"jest.pathToConfig": "**/jest.config.js",
"jest.showCoverageOnLoad": true
}
2) Extension ์ถ๊ฐ
3) Ctrl+Shift + P
sum function
export function sum(x: number, y: number) {
return x + y;
}
xx.test.ts ๋๋ xx.spec.ts > test, spec ๋ ๋ค ๋จ
test("sum", () => {
expect(sum(1, 2)).toBe(3);
expect(sum(1, 2)).not.toBe(2);
});
toBe : ์์ ๊ฐ ๋น๊ต
not.toBe : toBe์ ๋ถ์ ํ
~ 2025.02.03