【问题标题】:TypeError: environment.dispose is not a function when running npm testTypeError: environment.dispose 不是运行 npm test 时的函数
【发布时间】:2018-08-19 08:25:40
【问题描述】:

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

TypeError: environment.dispose 不是函数

在 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。为什么会出现此错误以及如何解决?

【问题讨论】:

  • 试试 npm install jest-cli
  • @VivekN 现在我得到--watch is not supported without git/hg, please use --watchAll
  • 在你的代码所在目录做 git init

标签: reactjs npm jestjs enzyme


【解决方案1】:

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

1) install jest-cli - 这将解决 TypeError: environment.dispose is not a function。

如果您遇到类似的错误

--watch is not supported without git/hg, please use --watchAll

做一个

git init 修复它。

希望对遇到类似问题的人有所帮助。

【讨论】:

  • 谢谢。为什么这里需要jest-cli
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多