2014-11-11 20 views
3

현재 Jest 테스트를 React 애플리케이션에 추가하려고합니다 (here). 그러나반응 라우터가 Jest (reactJs) 테스트를 중단하지 않음

, 나는 다음과 같은 테스트를 실행할 때,

/** @jsx React.DOM */ 

jest.dontMock('jquery'); 
jest.dontMock('../js/components/CategoryPage.jsx'); 
describe('Category Page', function() { 
    var React = require('react/addons'); 
    var TestUtils = React.addons.TestUtils; 
    var CategoryPage = require('../js/components/CategoryPage.jsx'); 
    it('renders into the page correctly', function() { 
    // Render the CategoryPage into the document 
    var categoryPage = TestUtils.renderIntoDocument(
     <CategoryPage params={{"category": "tests"}} /> 
    ); 
    expect(categoryPage).toBeDefined(); 
    }); 
}); 

나는 다음과 같은 오류 얻을 : 특별히 반응 라우터를 사용

● Category Page › it renders into the page correctly 
    - TypeError: Property 'makeHref' of object #<Object> is not a function 
     at Navigation.makeHref (/home/stephen/reps/node_modules/react-   router/modules/mixins/Navigation.js:29:25) 
     at React.createClass.getHref (/home/stephen/reps/node_modules/react-router/modules/components/Link.js:76:17) 
     at React.createClass.render (/home/stephen/reps/node_modules/react-router/modules/components/Link.js:97:18) 
     at ReactCompositeComponentMixin._renderValidatedComponent (/home/stephen/reps/node_modules/react/lib/ReactCompositeComponent.js:1260:34) 
     at wrapper [as _renderValidatedComponent] (/home/stephen/reps/node_modules/react/lib/ReactPerf.js:50:21) 
     at ReactCompositeComponentMixin.mountComponent  (/home/stephen/reps/node_modules/react/lib/ReactCompositeComponent.js:802:14) 
     at wrapper [as mountComponent] (/home/stephen/reps/node_modules/react/lib/ReactPerf.js:50:21) 
     at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (/home/stephen/reps/node_modules/react/lib/ReactMultiChild.js:195:42) 
     at ReactDOMComponent.Mixin._createContentMarkup (/home/stephen/reps/node_modules/react/lib/ReactDOMComponent.js:260:32) 
     at ReactDOMComponent.Mixin.mountComponent (/home/stephen/reps/node_modules/react/lib/ReactDOMComponent.js:182:14) 
     at ReactDOMComponent.wrapper [as mountComponent] (/home/stephen/reps/node_modules/react/lib/ReactPerf.js:50:21) 
     at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (/home/stephen/reps/node_modules/react/lib/ReactMultiChild.js:195:42) 
     at ReactDOMComponent.Mixin._createContentMarkup (/home/stephen/reps/node_modules/react/lib/ReactDOMComponent.js:260:32) 

내 응용 프로그램 모두와 CategoryPage 구성 요소를. CategoryPage는 반응 라우터를 인증에 사용하는 믹스 인을 포함합니다. 내 자신의 디버깅을 기반으로 Jest가 makeHref을 호출하려고 할 때 오류가 발생하는 것으로 나타났습니다.이 라우터는 반응 라우터의 기본 제공 탐색 방법 중 하나입니다.

이 문제를 해결하기 위해 처음에는 jest.dontMock('react-router')을 호출했지만 아무런 효과가 없었습니다. 문제는 CategoryPage을 모의하지 않음으로써 jest가 모든 종속성을 자동으로 돌이킬 수 없게 포함하고, 잠금 해제 된 것으로 보입니다.

이 문제를 해결하기가 어려운 이유 중 일부는 테스트와 관련된 것이 아니거나 Flux를 사용하고 상점 만 테스트하기 때문에 Reest와 함께 Jest를 사용하는 대부분의 사람들이 구성 요소를 테스트하지 않는 것처럼 보입니다. , Dispatchers, 등등. 우리는 아직 Flux를 사용하지 않기 때문에 이것은 우리에게는 선택 사항이 아니지만 앞으로 전환해야하는 부분 일 수 있습니다.

편집 1 : jest.dontMock('../js/components/CategoryPage.jsx')을 제거하면 테스트가 통과하지만 실제로 해당 구성 요소의 기능을 테스트하는 것은 불가능합니다.

편집 2 :

Category Page › it encountered a declaration exception 
- TypeError: 
/home/stephen/reps/js/components/CategoryPage.jsx: 
/home/stephen/reps/js/components/Feed.jsx:  
/home/stephen/reps/js/components/InvestmentButton.jsx: 
/home/stephen/reps/js/components/Modal.jsx: 
/home/stephen/reps/js/mixins/BootstrapModalMixin.jsx: 
/home/stephen/reps/node_modules/bootstrap/dist/js/npm.js: 
/home/stephen/reps/node_modules/bootstrap/js/alert.js: Cannot call method 'on' of undefined 

편집 : 3 : 내가 jest.dontMock('jquery')을 제외하면 나는 조동사를 만드는 데 사용하는 믹스 인에 관한 또 다른 오류가 나는 겉으로 곳을의 탐색 믹스 인 라우터 반응하는 버그를 격리했다 this.context.makeHref으로 전화하십시오. React 팀은 버전 .9 이후로 this.context을 사용하지 않으므로 문제의 원인이 될 수 있습니다. 따라서 this.context의 해결 방법이나 수정 사항은 환영합니다.

+0

난 당신이'반응-router' GitHub의 문제 페이지 https://github.com/rackt/react-router/issues – trekforever

답변

1

여기에 발견 된 문제를 만든 후 rackt-router 작성자의 도움을 받아이 문제를 해결했습니다. https://github.com/rackt/react-router/issues/465.

나는이 응용 프로그램을 테스트 할 때 Karma와 Jasmine을 사용하여이 문제를 해결했습니다. 그런 다음 여기에 스텁 함수 makeStubbedDescriptor를 사용했습니다 : https://gist.github.com/rpflorence/1f72da0cd9e507ebec29.

+0

요지의 링크가 깨진, [테스트 가이드상의 문제로 제기해야한다고 생각 ] (https://github.com/rackt/react-router/blob/0.13-compat/docs%2Fguides%2Ftesting.md)에는 필요한 코드 –

+0

@EugeneSafronov가 있어야합니다. 테스트 가이드 링크가 깨졌습니다. ( – Gezim

2

나는 계속해서 Jest를 사용할 수있는 수정 프로그램을 작성했다.

https://labs.chie.do/jest-testing-with-react-router/

+0

도움이됩니다. 그것은 몇 가지 문제가 발생할 수 있지만 ... 어떤 아이디어이 있다면 http://stackoverflow.com/questions/32462730/nextstate-on-componentwillupdate-not-correct-while-testing-with-jest-using-also 관련 수 있습니다 ? –

관련 문제