我正在为我的应用程序创建测试,并且在 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 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'
我不知道为什么它可能只在测试时失败,如果有人能指出我正确的方向,我会很高兴。