22.10.26 μμ μκ° μ€ κ³΅λΆν λ΄μ©λ€μ μ 리νμμ΅λλ€ π
νΌλλ°±μ μΈμ λ νμμ λλ€! π
Jestλ μμ΄μ½μ΄ μ¬λ―Έμκ² μ겨μ ν λ²μ―€μ μ λλ‘ μμλ³΄κ³ μΆμλ framework μ λλ€.
μμλ‘ μ λ°μ²λΌ, μ¬λ―Έμμ κ²λ§ κ°μ Jest!
μ μ΄μ νꡬν΄λ³΄λ¬ GO GO!
Jestλ Facebookμμ λ§λ κ°λ¨νκ³ μ¬μ©νκΈ° μ¬μ΄ testing Frameworkμ λλ€!
npm i -D jest @types/jest
@types/jest
ν¨ν€μ§λ₯Ό μ€μΉνλ©΄ VSCodeμμ μ½λ μλ μμ± κΈ°λ₯μ μ¬μ© κ°λ₯ν©λλ€!
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
Jest ν
μ€ν
λꡬλ __tests__
λλ ν 리 μμ js
νμ₯μλ₯Ό κ°μ§ νμΌ λλ .(test|spec).js
νμ₯μλ₯Ό κ°μ§ νμΌμ ν
μ€νΈ νμΌλ‘ μΈμν©λλ€.
μλ₯Ό λ€λ©΄, __tests__
λλ ν 리λ₯Ό λ³λλ‘ κ΅¬μ± κ΄λ¦¬ν μ μκ³ ,
μλλ©΄ ν
μ€νΈ ν νμΌμ μ€μ μ½λμ κ°μ μμΉμμ κ΄λ¦¬ν μλ μμ΅λλ€.
β μ΄ λλ 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"
}
}
β νμν μ’ μμ± ν¨ν€μ§λ₯Ό μ€μΉνλ©΄ λ©λλ€!
Babel κ΅¬μ± νμΌμ νλ‘μ νΈ λ£¨νΈ μμΉμ μμ±ν ν, νμ¬ λ²μ μ Nodeλ₯Ό λμμΌλ‘ μ€μ ν©λλ€!
npm i -D babel-jest @babel/core @babel/preset-env
module.exports = {
presets: [
['@babel/preset-env', {targets: { node: 'current' }}]
],
};
module.exports = {
testEnvironment: 'jsdom',
transform: {
'\\.js$': 'babel-jest',
},
testPathIgnorePatterns: ['/node_modules/'],
setupFilesAfterEnv: ['./jest.setup.js'],
}
import 'regenerator-runtime';