[FE - ConnecTo] DAY73 TILπŸ‘©πŸ»β€πŸ’» (Jest νƒκ΅¬ν•˜κΈ°πŸ€Ήβ€β™‚οΈ - 1편)

JUNYΒ·2022λ…„ 10μ›” 26일
0

πŸ“šZeroBase ConnecTo Front-End

λͺ©λ‘ 보기
53/53
post-thumbnail

22.10.26 μˆ˜μ—… μ‹œκ°„ 쀑 κ³΅λΆ€ν•œ λ‚΄μš©λ“€μ„ μ •λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€ 😊
ν”Όλ“œλ°±μ€ μ–Έμ œλ‚˜ ν™˜μ˜μž…λ‹ˆλ‹€! 🍊

JestλŠ” μ•„μ΄μ½˜μ΄ 재미있게 μƒκ²¨μ„œ ν•œ λ²ˆμ―€μ€ μ œλŒ€λ‘œ μ•Œμ•„λ³΄κ³  μ‹Άμ—ˆλ˜ framework μž…λ‹ˆλ‹€.

μ‚μ—λ‘œ μ‹ λ°œμ²˜λŸΌ, μž¬λ―Έμžˆμ„ κ²ƒλ§Œ 같은 Jest!

자 이제 νƒκ΅¬ν•΄λ³΄λŸ¬ GO GO!

Jest?

JestλŠ” Facebookμ—μ„œ λ§Œλ“  κ°„λ‹¨ν•˜κ³  μ‚¬μš©ν•˜κΈ° μ‰¬μš΄ testing Frameworkμž…λ‹ˆλ‹€!


μ„€μΉ˜λŠ” μ–΄λ–»κ²Œ ν•˜λ‚˜μš”?

npm i -D jest @types/jest

@types/jest νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜ν•˜λ©΄ VSCodeμ—μ„œ μ½”λ“œ μžλ™ μ™„μ„± κΈ°λŠ₯을 μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€!


Jest ꡬ성 νŒŒμΌμ€ μ–΄λ–»κ²Œ μƒμ„±ν•˜λ‚˜μš”?

npx jest --init

πŸ•Ή Jest μ„€μΉ˜μ‹œ λ‚˜μ˜€λŠ” μ§ˆλ¬Έμ€ λ‹€μŒκ³Ό 같이 μ„€μ •ν•˜λ©΄ λ©λ‹ˆλ‹€!

# λ‹€μŒ μ§ˆλ¬Έμ€ Jestκ°€ ν”„λ‘œμ νŠΈμ— μ ν•©ν•œ ꡬ성을 λ§Œλ“œλŠ” 데 도움이 λ©λ‹ˆλ‹€.
The following questions will help Jest to create 
a suitable configuration for your project

# "package.json"μ—μ„œ "test" 슀크립트λ₯Ό μ‹€ν–‰ν•  λ•Œ Jestλ₯Ό μ‚¬μš©ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?
βœ” Would you like to use Jest when running "test" script in "package.json"?
… yes

# ꡬ성 νŒŒμΌμ— Typescriptλ₯Ό μ‚¬μš©ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?
βœ” Would you like to use Typescript for the configuration file? 
… no

# ν…ŒμŠ€νŠΈμ— μ‚¬μš©ν•  ν…ŒμŠ€νŠΈ ν™˜κ²½ 선택
βœ” Choose the test environment that will be used for testing 
β€Ί jsdom (browser-like)

# Jestκ°€ 컀버리지 λ³΄κ³ μ„œλ₯Ό μΆ”κ°€ν•˜κΈ°λ₯Ό μ›ν•˜μ‹­λ‹ˆκΉŒ?
Do you want Jest to add coverage reports?
… no

# 컀버리지 λͺ©μ μœΌλ‘œ μ½”λ“œλ₯Ό κ³„μΈ‘ν•˜λ €λ©΄ μ–΄λ–€ κ³΅κΈ‰μžλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆκΉŒ?
Which provider should be used to instrument code for coverage?
β€Ί babel

# λͺ¨λ“  ν…ŒμŠ€νŠΈ 전에 λͺ¨μ˜ 호좜, μΈμŠ€ν„΄μŠ€, μ»¨ν…μŠ€νŠΈ 및 κ²°κ³Όλ₯Ό μžλ™μœΌλ‘œ μ§€μš°μ‹œκ² μŠ΅λ‹ˆκΉŒ?
Automatically clear mock calls, instances, contexts & results before every test?
… no

ν…ŒμŠ€νŠΈ λͺ…λ Ήμ–΄λŠ” μ–΄λ–»κ²Œ λ“±λ‘ν•˜λ‚˜μš”?

package.json νŒŒμΌμ— Jest ν…ŒμŠ€νŠΈλ₯Ό μˆ˜ν–‰ν•  수 μžˆλŠ” λͺ…λ Ήμ–΄λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€!

{
	"scripts": {
		"test": "jest --watch"
	}
}
# ν…ŒμŠ€νŠΈ 파일 λ³€κ²½ κ΄€μ°° ν›„, ν…ŒμŠ€νŠΈ κ²°κ³Ό 좜λ ₯
npm test

testν•  νŒŒμΌμ€ μ–΄λ–»κ²Œ κ΄€λ¦¬ν•˜λ‚˜μš”?

Jest ν…ŒμŠ€νŒ… λ„κ΅¬λŠ” __tests__ 디렉토리 μ•ˆμ— js ν™•μž₯자λ₯Ό 가진 파일 λ˜λŠ” .(test|spec).js ν™•μž₯자λ₯Ό 가진 νŒŒμΌμ„ ν…ŒμŠ€νŠΈ 파일둜 μΈμ‹ν•©λ‹ˆλ‹€.

예λ₯Ό λ“€λ©΄, __tests__ 디렉토리λ₯Ό λ³„λ„λ‘œ ꡬ성 관리할 수 있고,

μ•„λ‹ˆλ©΄ ν…ŒμŠ€νŠΈ ν•  νŒŒμΌμ„ μ‹€μ œ μ½”λ“œμ™€ 같은 μœ„μΉ˜μ—μ„œ 관리할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.


λ§Œμ•½ Jest와 ESLint λ¦°νŒ… 도ꡬλ₯Ό ν•¨κ»˜ μ‚¬μš©ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Όν•˜λ‚˜μš”?

β†’ 이 λ•ŒλŠ” eslint-plugin-jest νŒ¨ν‚€μ§€λ„ ν•¨κ»˜ μ„€μΉ˜ν•˜λ©΄ λ©λ‹ˆλ‹€!

npm i -D eslint-plugin-jest

κ·Έ λ‹€μŒ, .eslintrc νŒŒμΌμ„ λ§Œλ“  ν›„, ꡬ성 μ½”λ“œλ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.

npx eslint --init
{
  "env": {
    "es2021": true,
    "node": true,
    "jest/globals": true
  },
  "settings": {
    "jest": { "version": 26 }
  },
  "plugins": [ "jest" ],
  "extends": [
    "eslint:recommended",
    "plugin:jest/recommended"
  ],
  "parserOptions": {
    "ecmaVersion": 12
  },
  "rules": {
    "indent": [ "error", 2 ],
    "linebreak-style": [ "error", "unix" ],
    "quotes": [ "error", "single", { "allowTemplateLiterals": true } ],
    "semi": [ "error", "always" ],
    "jest/no-disabled-tests": "warn",
    "jest/no-focused-tests": "error",
    "jest/no-identical-title": "error",
    "jest/prefer-to-have-length": "warn",
    "jest/valid-expect": "error"
  }
}

Jest ν™˜κ²½μ—μ„œ Babel 컴파일러λ₯Ό ν•¨κ»˜ μ‚¬μš©ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Όν•˜λ‚˜μš”?

β†’ ν•„μš”ν•œ 쒅속성 νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜ν•˜λ©΄ λ©λ‹ˆλ‹€!

Babel ꡬ성 νŒŒμΌμ„ ν”„λ‘œμ νŠΈ 루트 μœ„μΉ˜μ— μƒμ„±ν•œ ν›„, ν˜„μž¬ λ²„μ „μ˜ Nodeλ₯Ό λŒ€μƒμœΌλ‘œ μ„€μ •ν•©λ‹ˆλ‹€!

npm i -D babel-jest @babel/core @babel/preset-env
  • babel.config.js
module.exports = {
	presets: [
		['@babel/preset-env', {targets: { node: 'current' }}]
	],
};
  • jest.config.js
module.exports = {
	testEnvironment: 'jsdom',
	transform: {
    '\\.js$': 'babel-jest',
  },
	testPathIgnorePatterns: ['/node_modules/'],
	setupFilesAfterEnv: ['./jest.setup.js'],
}
  • jest.setup.js
import 'regenerator-runtime';
profile
μ„±μž₯ν•˜λŠ” 개발자🌼

0개의 λŒ“κΈ€