[node]jest에서 import가 안되는 오류 해결 방법

Yu River·2022년 6월 19일
0

Node

목록 보기
1/1

jest에서 import가 안되는 오류 발생

노드 토이 프로젝트 구현 중에 jest로 단위 테스트를 진행하는데 import하는 부분에서 다음과 같은 오류가 났다.

 FAIL  src/test/service/getProductInfoService.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/yuha/Desktop/discord_github/toy_compare_prc/compare_prc/node_modules/got/dist/source/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import create from './create.js';
                  
    SyntaxError: Cannot use import statement outside a module

    > 1 | const got = require('got');
        | ^
      2 | const host = 'kream.co.kr';
      3 | const port = 80;
      4 | const protocol = 'http';

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (src/lib/kream.js:1:1)

...요약하자면
" 너가 쓰려고 하는 got 라이브러리 안에 내장되어있는 여러 라이브러리를 import 하는부분에서 오류가 나고 있어 ~!😅 "
라고 말하고 있는건데 여기저기 찾아보니 jest가 import 구문(ES6)을 지원하지 않는다고 한다.
그래서 ES6 코드를 babel 라이브러리르 통해 ES5로 변환해야 한다고 다들 입을 모아 말하고 있다.

jest에서 import 오류 해결 방법

1.

https://velog.io/@noyo0123/jest-test에서-import-를-못-쓰네요-pik230v1hp

2.

https://velog.io/@njh7799/typescript-jest로-test할-때-import-사용하기

3.

하지만 난 위 두가지 해결 방법도 먹히질 않았다.
babel을 설치해서 설정 옵션을 계속 바꿔보기도하고
jest에서 ES6을 지원해주는 모듈들을 따로 찾아 설치해보기도 하고
다 해봤지만 ㅠㅠ 되지를 않아서 나는 결국 got라이브러리의 버전을 다운그레이드 시켜서
강제로 ES5 버전의 got을 적용시켰다.

$ npm install got@11.8.2
profile
도광양회(韜光養晦) ‘빛을 감추고 어둠속에서 힘을 기른다’

0개의 댓글