๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

NodeJS, Jest, VSCode debugging

๋ฌธ์ œ ์ƒํ™ฉ#

2020-09-11-nodejs-jest-vscode-debugging-image-0

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํด๋” ๋‚ด๋ถ€์˜ solution.js ํŒŒ์ผ์— ์ค‘๋‹จ์ ๋ฅผ ๊ฑธ๊ณ  ํ™˜๊ฒฝ์„ Node.js๋กœ ์„ค์ •ํ•˜๋ฉด ๋””๋ฒ„๊น…์ด ๋ ๊ฑฐ๋ผ ๊ธฐ๋Œ€ํ–ˆ๋Š”๋ฐ ์•„๋ฌด ์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์•˜๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-1

๋””๋ฒ„๊ทธ ์ฝ˜์†”์„ ๋ณด๋‹ˆ node๋กœ solution.js ๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ, solution ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์„ ์–ธ๋งŒ ํ•˜๊ณ  ๋๋‚œ ๋“ฏ ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด solution.test.js ์—์„œ solution ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋‹ˆ, solution.test.js ํŒŒ์ผ์— ์ค‘๋‹จ์ ์„ ๊ฑธ์–ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-2

test ํŒŒ์ผ์— ์ค‘๋‹จ์ ์„ ๊ฑธ์—ˆ๋”๋‹ˆ describe ํ•จ์ˆ˜๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-3

ReferenceError: describe is not defined NodeJs

Debug single javascript file in "Visual Studio Code"

stackoverflow๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ๋•Œ node ๋Œ€์‹  mocha ๋กœ ์‹คํ–‰ํ•ด์•ผํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ํ˜„์žฌ jest๋ฅผ ์“ฐ๊ณ  ์žˆ๋Š”๋ฐ jest๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ผ ๋“ฏ ์‹ถ์—ˆ๋‹ค.

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ vscode์˜ ๋””๋ฒ„๊ทธ ๋ชจ๋“œ๋ฅผ jest๋กœ ์‹คํ–‰์‹œํ‚ค๋Š”์ง€ ๊ถ๊ธˆํ–ˆ๋‹ค. stackoverflow์˜๋‹ค๋ฅธ ๋‹ต๋ณ€๋„ ๋” ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ .vscode/launch.json ์„ ์„ค์ •ํ•ด์„œ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-4

launch.json ์˜ program ์— ์‹คํ–‰ํ•  jest ๋ฅผ ๋„ฃ๊ณ  args ๋กœ test ํŒŒ์ผ ๋˜๋Š” test ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-5

launch.json ๋งŒ๋“ค๊ธฐ

{    "version": "0.2.0",    "configurations": [        {            "type": "node",            "request": "launch",            "name": "jest ๋””๋ฒ„๊ทธ",            "skipFiles": ["<node_internals>/**"],            "program": "${workspaceFolder}/node_modules/jest/bin/jest.js",            "args": ["${relativeFileDirname}"]        }    ]}

VSCode ๋ณ€์ˆ˜( ${relativeFileDirname} ) ๋กœ ํ˜„์žฌ ์—ด๋ ค์žˆ๋Š” ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฑธ args ๋กœ ๋„˜๊ธฐ๋ฉด jest ./foo/bar/directory ๊ฐ€ ์™„์„ฑ๋œ๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-6

์ด์ œ ์—ด๋ ค์žˆ๋Š” solution.js ์—์„œ ์ค‘๋‹จ์ ์„ ์žก๊ณ  F5 ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด solution.js ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ jest ๋กœ ๋””๋ฒ„๊ทธ ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•œ๋‹ค. solution.js ์™€ solution.test.js ํŒŒ์ผ์ด ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— test ์ฝ”๋“œ๊ฐ€ ๋””๋ฒ„๊ทธ ๋ชจ๋“œ๋กœ ์‹คํ–‰๋˜๊ณ , ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ์ธํ’‹์œผ๋กœ solution ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.


2020-09-11-nodejs-jest-vscode-debugging-image-7

๋””๋ฒ„๊ทธ ์ฝ˜์†”์„ ๋ณด๋‹ˆ node ๋กœ jest ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , args ๋กœ ํ˜„์žฌ ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋„˜๊ธด๋‹ค.


์ฐธ๊ณ ์ž๋ฃŒ#

Troubleshooting ยท Jest

Visual Studio Code Variables Reference

Debugging in Visual Studio Code