FrontEngineer JungBam

jest와 axios 호환 이슈 본문

개발일지

jest와 axios 호환 이슈

정밤톨 2023. 6. 21. 00:52
jest를 이용하여 test코드를 작성하면 axios import 이슈가 발생한다.

axios 깃헙에 jest test관련 이슈를 확인하면 해결방법이 몇가지 명시되어 있는데 해결방법을 해본 결과 버전을 다운하는 것 외에 다른 방법은 문제가 해결되지 않았다.

 

Updating Axios from 0.27.2 to 1.0.0 breaks Jest tests in a create-react-app app · Issue #5026 · axios/axios

I have several React applications created with a Typescript based create-react-app application (and using react-scripts@5.0.1, which depends on jest@27.4.3). Attempting to upgrade Axios from 0.27.2...

github.com

제시된 해결방안

1. axios 버전 다운그레이드
- npm uninstall axios  // 기존에 설치된 axios 제거
- npm install axios@0.27.0

2. 설정을 통한 경로 우회
// jest.config.js
module.exports = {
  // ...기타 설정
  moduleNameMapper: {
    '^axios$': '<rootDir>/path/to/axios-mock.js',
  },
};​


3. 설정을 통한 axios 무시

module.exports = {
  transformIgnorePatterns: ['/node_modules/(?!(axios)/)'],
};


4. package.json 파일 설정 변경

test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"",

 

반응형
Comments