programing

Create-React-app 프로젝트를 테스트할 때 "ReferenceError: 문서가 정의되지 않음"

prostudy 2022. 3. 10. 22:43
반응형

Create-React-app 프로젝트를 테스트할 때 "ReferenceError: 문서가 정의되지 않음"

이것은 나의 것이다.__tests__/App.js파일:

import React from 'react';
import ReactDOM from 'react-dom';
import App from '../src/containers/App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
});

// sanity check
it('one is one', () => {
  expect(1).toEqual(1)
});

그리고 이것은 내가 실행할 때 얻는 출력이다.yarn test:

FAIL  __tests__/App.js
  ● renders without crashing

    ReferenceError: document is not defined

      at Object.<anonymous>.it (__tests__/App.js:6:15)

  ✕ renders without crashing (1ms)
  ✓ one is one

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        0.128s, estimated 1s
Ran all test suites related to changed files.

다음에 대해 다른 모듈을 가져와야 하는가?document여기서 이용할 수 있으시겠습니까?

도와줘서 고마워!

나한테는 둘 중 하나가 효과가 있었어

포장되어.json 파일 추가 테스트 스크립트 Environ 플래그 추가

"scripts": {
   "test": "react-scripts test --env=jsdom"
}

효소사용

 import { shallow } from 'enzyme';

 it('renders without crashing', () => {
  shallow(<App />);
 });

장치 테스트 소스 상단에 설명 배치

/**
 * @jest-environment jsdom
 */

문서와 창을 모의 실험하라는 신호를 보낸다.

값을 설정했다jest.config.js아래와 같이 작동했다.

testEnvironment: 'jsdom'

소포 안에.농담이라면 json

"scripts":{
      "test": "jest --env=jsdom"
}

나는 2021년 9월에 이 이슈를 우연히 만났다.반응하여 테스트를 배우고 있으며 node_modules 폴더를 입력하여 이 문제를 해결하였다.그 폴더 안에는 농담 폴더가 있다.농담 폴더 안에는 소포가 있다.json 파일그 꾸러미 안에.json file 나는 한 가지를 삽입했다.위에서 언급한 것과 유사하다.

꾸러미json 파일

"testEnvironment": "jsdom"

나는 일반적으로 node_modules 폴더를 편집해서는 안 된다는 것을 알고 있다. 그러나 이것을 계속 배우려면 그렇게 해야 할 것이다.

좀 더 심각한 프로젝트에 다른 해결책이 필요한지 모르겠다.

사용 중인 경우create-react-app, 꼭 뛰어야 한다.yarn test대신에yarn jest아니면 다른 것.

이것을 인덱스.test.js(또는 테스트 파일) 위에 놓으십시오.가운데나 끝에 붙이지 마라.

/*** * @jest-filename jsdom */

참조URL: https://stackoverflow.com/questions/43901660/referenceerror-document-is-not-defined-when-trying-to-test-a-create-react-app

반응형