【问题标题】:Make react-router not break Jest (reactJs) tests使 react-router 不会破坏 Jest (reactJs) 测试
【发布时间】:2015-01-08 08:01:36
【问题描述】:

我目前正在尝试将 Jest 测试添加到 React 应用程序(找到 here)。

但是,当我运行以下测试时,

/** @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 组件都专门使用react-router。 CategoryPage 包含一个使用 react-router 进行身份验证的 mixin。根据我自己的调试,我发现当 Jest 尝试调用 makeHref(react-router 的内置导航方法之一)时发生错误。

为了解决这个问题,我首先尝试调用jest.dontMock('react-router'),但这没有任何效果。问题似乎在于,通过不模拟 CategoryPage,jest 将自动且不可逆转地包含其所有依赖项,未模拟。

这个问题如此难以解决的部分原因是因为大多数使用 Jest 和 React 的人似乎没有测试他们的组件,要么是因为他们不是以测试为中心,要么是因为他们使用 Flux 并且只测试 Store,调度程序等。我们还没有使用 Flux,所以这不是我们的选择,但可能是我们将来必须过渡到的东西。

编辑 1:如果我删除 jest.dontMock('../js/components/CategoryPage.jsx'),则测试通过,但无法实际测试该组件的功能。

编辑 2:当我排除 jest.dontMock('jquery') 时,我得到另一个与我用来创建模态的 mixin 相关的错误:

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:我似乎已将错误隔离到 react-router 的 Navigation mixin,它调用 this.context.makeHref。 React 团队从 0.9 版开始弃用了this.context,所以我相信这可能是问题的根源。因此,欢迎对this.context 进行任何变通或修复。

【问题讨论】:

标签: javascript reactjs react-jsx jestjs react-router


【解决方案1】:

在创建此处发现的问题后,我在 rackt-router 的创建者的帮助下最终解决了这个问题:https://github.com/rackt/react-router/issues/465

我通过使用 Karma 和 Jasmine 测试我的应用程序解决了这个问题。然后我使用了在这里找到的存根函数 makeStubbedDescriptor:https://gist.github.com/rpflorence/1f72da0cd9e507ebec29

【讨论】:

  • gist 的链接已损坏,testing guide 应该有所需的代码
  • @EugeneSafronov,测试指南链接现已损坏:(
【解决方案2】:

我继续进行了修复,让您仍然可以使用 Jest。

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

【讨论】:

猜你喜欢
  • 2017-11-22
  • 2015-10-12
  • 2019-08-05
  • 2017-11-26
  • 2019-05-02
  • 2017-11-10
  • 2018-09-21
  • 2019-02-20
  • 1970-01-01
相关资源
最近更新 更多