reactjs - 运行 npm test 时出现 TypeError : environment. dispose 不是函数

标签 reactjs npm jestjs enzyme

当我尝试在我的reactjs项目中运行测试时,我得到

TypeError: environment.dispose is not a function

at Promise.resolve.then

(node_modules/react-scripts/node_modules/jest/node_modules/jest-cli/build/runTest.js:102:17)

package.json

{
  "name": "wall-app-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "auth0-js": "^9.3.2",
    "axios": "^0.18.0",
    "jwt-decode": "^2.2.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.1",
    "redux": "^3.7.2",
    "redux-form": "^7.3.0",
    "redux-saga": "^0.16.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "jest": "^22.4.2",
    "jest-enzyme": "^5.0.3",
    "jsdom": "^11.6.2"
  }
}

我的测试

import { PostsIndex } from '../posts_index'
import { shallow } from 'enzyme';
import React from 'react';

describe('Posts Index Component', () => {

  let wrapper;
  const mockFetchPostsFn = jest.fn();

  beforeEach(() => {
    wrapper = shallow(<PostsIndex fetchPosts={mockFetchPostsFn}/>)
  })

  describe('When the form is submitted', () => {
    it('should call the mock login function', () => {
      expect(mockFetchPostsFn.mock.calls.length).toBe(1)
    })
  })
})

我正在测试的组件PostsIndex

import React, { Component } from 'react';
import { fetchPosts } from '../actions/post_actions';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import _ from 'lodash';


export class PostsIndex extends Component {
  componentDidMount() {
    this.props.fetchPosts();
  }

  renderPosts() {
    return _.map(this.props.posts, (post) => {
      return(
        <li className="list-group-item" key={post.id}>
        <Link to={`/post/${post.id}`}>
          {post.content}
        </Link>
        </li>
      )
    })
  }

  render() {
    const { posts } = this.props
    if (posts.isLoading) {
      return (
          <div>
            <h3>Loading...</h3>
          </div>
      )
    } else if (posts.error) {
      return (
        <div>
          <h3>Error getting posts</h3>
          <h2>Status code {JSON.stringify(posts.error)}</h2>
        </div>
      )
    } else {
      return (
        <div>
          <div className="text-xs-right">
            <Link className="btn btn-primary" to="/posts/new">
              Add a Post
            </Link>
          </div>
          <h3>Posts</h3>
          <ul className="list-group">
            {this.renderPosts()}
          </ul>
        </div>
      );
    }
  }
}

function mapStateToProps({ posts }){
  return { posts }
}

export default connect(mapStateToProps, { fetchPosts })(PostsIndex);

setupTests.js

const Enzyme = require('enzyme');
const EnzymeAdapter = require('enzyme-adapter-react-16');
Enzyme.configure({ adapter: new EnzymeAdapter() });

我正在使用 create-react-app。为什么我会收到此错误以及如何修复它?

最佳答案

为了记录答案,以便任何有类似问题的人都可以解决它。 这是解决方案:-

1) 安装 jest-cli - 这将解决 TypeError:environment.dispose 不是函数。

如果您遇到类似错误

--watch 不支持 git/hg,请使用 --watchAll

做一个

git init 来修复它。

希望它可以帮助任何遇到类似问题的人。

关于reactjs - 运行 npm test 时出现 TypeError : environment. dispose 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49216222/

相关文章:

javascript - 在react.js中过滤记录

npm - 错误 : Can't resolve 'stream' in . ... papaparse

javascript - react + Redux : Uncaught Error: Expected the reducer to be a function

javascript - 在自定义事件上使用 enzyme 模拟?

reactjs - 主题 react Multi-Tenancy 应用程序

javascript - jest-jasmine2 env.js 未处理的错误

reactjs - Jest/ enzyme : Error: Uncaught [TypeError: Cannot read property 'query' of undefined] on component wrapped in withRouter

Angular 8 的 Angular Material 版本

javascript - NodeJS 和 NPM : Package Security

javascript - 关于 React js enzyme 中的状态