【发布时间】:2017-10-03 18:48:48
【问题描述】:
我正在为我的应用程序创建测试,但在 Jest 中运行它们时遇到问题。我的代码结构是这样的:
<pre>
./src/
├── actions
│ ├── departments.js
│ ├── departments.test.js
├── actionTypes
│ ├── departmentsTypes.js
├── components
│ ├── common
│ │ ├── Form
│ │ │ ├── Form.css
│ │ │ ├── FormElement
│ │ │ │ ├── FormElement.css
│ │ │ │ ├── FormElement.js
│ │ │ │ ├── FormElement.test.js
│ │ │ │ └── __snapshots__
│ │ │ ├── Form.js
│ │ │ ├── Form.test.js
│ │ │ ├── index.js
│ │ │ └── __snapshots__
│ │ │ └── Form.test.js.snap
│ │ ├── index.js
│ │ ├── SchemaFormFactory
│ │ │ ├── SchemaFormFactory.js
│ │ │ ├── SchemaFormFactory.test.js
│ │ │ └── __snapshots__
│ │ │ └── SchemaFormFactory.test.js.snap
│ │ └── TextInput
│ ├── DepartmentSelector
│ │ ├── DepartmentSelector.css
│ │ ├── DepartmentSelector.js
│ │ ├── DepartmentSelector.test.js
│ │ ├── index.js
│ │ └── __snapshots__
│ ├── index.js
│ ├── MainApp
│ │ ├── index.js
│ │ ├── MainApp.css
│ │ ├── MainApp.js
│ │ ├── MainApp.test.js
│ │ └── __snapshots__
├── containers
│ ├── DepartmentForm
│ │ └── DepartmentForm.js
│ ├── DepartmentsSearch
│ │ ├── DepartmentsSearch.js
│ │ ├── DepartmentsSearch.test.js
│ │ ├── index.js
│ │ └── __snapshots__
├── helpers
│ └── test-helper.js
├── index.js
├── reducers
│ ├── departments.js
│ ├── departments.test.js
</pre>
我正在尝试对 FormElement (FormElement.test.js) 组件进行测试。在测试里面我有一个import语句:
import DepartmentsSearch from '../../../../containers/DepartmentsSearch/DepartmentsSearch'
我的 DepartmentSearch 是一个使用 react-redux 库中的连接的容器。
import DepartmentSelector from '../../components/DepartmentSelector/DepartmentSelector'
import {createDepartment} from '../../actions'
const mapStateToProps = (state) => {
return {
departments: state.departments
}
}
export default connect(mapStateToProps, {createDepartment})(DepartmentSelector)
由于某种原因import DepartmentSelector 返回 undefined 而不是 react 组件(它只是一个哑组件而不是容器)。最奇怪的是,只有在运行测试时才会发生这种情况,而不是在浏览器中运行代码时发生。我一开始就尝试使用顶级导入,但也失败了。
import {DepartmentSelector} from '../../components'
我不知道为什么它可能仅在测试时失败,如果有人能指出我正确的方向,我会很高兴。
【问题讨论】:
标签: javascript reactjs testing redux jestjs