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.
상황
Next.js + Jest + testing-libaray 가 깔려있는 상황에서, 컴포넌트 테스트코드 작성 중에 발생
원인
jest 모듈은 별다른 설정을 해주지 않으면 테스트 대상의 코드가 자바스크립트일 것이라 생각한다.
특히나 jest는 import export 구문 ES6 을 지원하지 않는다 라고 한다.
그래서 여러 문서를 찾아보면, ES6 코드를 Babel 라이브러리로 ES5 로 변환해야 한다고 한다.
그러나 난... babel 을 쓰고있지 않아서 굳이 설치하고싶지 않았다.
그래서 찾은게, 테스트환경을 jest-environment-jsdom 으로 변경하는 것.
jest 기본환경은 node.js. 하지만 웹앱을 구축하는 경우 브라우저와 같은 환경인 jsdom 을 사용할 수 있다.
yarn add jest-environment-jsdom -D
const customJestConfig = {
...
testEnvironment: 'jest-environment-jsdom', // 테스트 환경 변경
}
출처:
https://jestjs.io/docs/configuration
'🌳Frontend > error log' 카테고리의 다른 글
[ERROR] command not found: nvm (2) | 2023.05.26 |
---|---|
[ERROR] TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. (0) | 2023.05.23 |
[ERROR] Error: Invalid src prop .. is not configured under images in your (0) | 2023.05.18 |
jest 테스트코드 작성시 Cannot find module (2) | 2023.03.22 |
yarn install Fetching packages... 멈춤현상 (0) | 2023.03.22 |