When I try to run the tests in my reactjs project, I get
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"
}
}
my test
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)
})
})
})
The component PostsIndex, which I am testing
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() });
I'm using create-react-app. Why am I getting this error and how to I fix it?
In order to document the answer, so anyone having a similar problem can fix it. Here is the solution:-
1) install jest-cli
- This would resolve the TypeError: environment.dispose is not a function.
If you are facing an error like
--watch is not supported without git/hg, please use --watchAll
Do a
git init
to fix it.
Hope it helps anyone coming with a similar problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With