๊ฐœ์ธ๊ณต๋ถ€/TDD

Jest ์ดˆ๊ธฐํ™˜๊ฒฝ ์„ค์ •์—์„œ ์—๋Ÿฌํ•ธ๋“ค๋ง

soon327 2021. 9. 13. 21:19


๐Ÿ’ฉ Jest ์ดˆ๊ธฐํ™˜๊ฒฝ ์„ค์ •์—์„œ์˜ ์—๋Ÿฌ๋“ค

ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ์„ ์œ„ํ•œ ๊ณต๋ถ€ ์ค‘ ๋ฐœ์ƒํ•œ jest ์ดˆ๊ธฐํ™˜๊ฒฝ ์„ค์ •์—์„œ์˜ ์—๋Ÿฌ๋“ค์„ ๊ธฐ๋กํ•˜๊ธฐ์œ„ํ•ด ๊ธ€์„ ์ž‘์„ฑํ•œ๋‹ค.

Unexpected token "<"

React์˜ JSX๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ ๋ฐœ์ƒํ•œ ํ…Œ์ŠคํŠธ์—๋Ÿฌ.

Error

Running coverage on untested files...Failed to collect coverage from /index.js
ERROR: /index.js: Unexpected token (52:2)

  50 |
  51 | render(
> 52 |   <Gallery images={images} />,
     |   ^

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•
@babel/preset-react ํ”„๋ฆฌ์…‹์„ ์„ค์น˜ํ•˜์—ฌ ์„ค์ •ํ•ด์ค€๋‹ค.

// babel.config.js
module.exports = {
  'presets': ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'],
  ...
};

ReferenceError: regeneratorRuntime is not defined

ํ…Œ์ŠคํŠธ์—์„œ async/await๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์˜€์„ ๋•Œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜

Error

ReferenceError: regeneratorRuntime is not defined

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

๋ฐ”๋ฒจ์ด async/await ์ฝ”๋“œ๋ฅผ regeneratorRuntime๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜์ด๋ฏ€๋กœ regeneratorRuntime์„ ๋ชจ๋“ˆ์„ ํฌํ•จํ•œ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค์น˜ํ•˜์—ฌ ์„ค์ •ํ•ด์ค€๋‹ค.

// babel.config.js
module.exports = {
  'presets': ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'],
  'plugins': ['@babel/plugin-transform-runtime'],
};

document is not defined

React-Testing-Library๋Š” DOMํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜

Error

    ReferenceError: document is not defined

       9 |   const setup = () => {
      10 |     const [Wrapper, store] = prepareMockReduxWrapper();
    > 11 |     render(
         |     ^
      12 |       <Wrapper>
      13 |         <TodoForm />
      14 |       </Wrapper>

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•
ํ…Œ์ŠคํŠธํ™˜๊ฒฝ์„ jsdom์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

//package.json
"scripts": {
    ...
    "test": "jest --env=jsdom",
    ...
  },

๐Ÿ™ ์ฐธ๊ณ