Errors

[jest] The error below may be caused by using the wrong test environment

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.
    
    ReferenceError: document is not defined

      11 |   const handleClick = jest.fn();
      12 |
    > 13 |   const { container, getByText } = render((
         |                                    ^
      14 |     <Item
      15 |       todo={todo}
      16 |       onClick={handleClick}

      at render (node_modules/@testing-library/react/dist/pure.js:83:5)
      at Object.<anonymous> (src/components/Item.test.jsx:13:36)

테스트 작성을 시도해보던 중 에러가 발생했다. 잘못된 테스트 환경이 사용된다?

 

https://github.com/testing-library/react-testing-library/issues/422

원인을 찾지 못하던 중 검색을 통해 위 링크를 참고하여 jest.config.js 파일을 수정했다.

 

다음을 추가 >> testEnvironment: 'jest-environment-jsdom'

 

하지만 굳이 아래와 같이 설정하지 않아도 잘 돌아가는 경우도 있었는데 그 둘간의 차이점은 발견하지 못했다.

module.exports = {
  testEnvironment: 'jest-environment-jsdom',
  setupFilesAfterEnv: [
    'jest-plugin-context/setup',
    './jest.setup',
  ],
  coverageThreshold: {
    global: {
      branches: 100,
      functions: 100,
      lines: 100,
      statements: 100,
    },
  },
};

 

'Errors' 카테고리의 다른 글

ModuleNotFoundError: No module named 'flask'  (0) 2022.01.25