Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write proper jest tests with HOC?

I’m trying to understand why my jest/enzyme tests are failing. I have a component that I use the compose function from redux in, structured as following:

class MyComponent extends Component {
    //code here
}

export default compose(
    connect(mapStateToProps),
)(MyComponent)

In my jest test, I do this:

Import { MyComponent } from ‘app/MyComponent’;

describe(‘<MyComponent />’, () => {
    let component;
    beforeEach(() => {
        props = {
            id: ‘23423’     
        }
        component = shallow(<MyComponent {…props} />);
    }


    it(‘Loads correctly’, () => {
        expect(component.state(‘isLoading’)).toEqual(true);
        expect(component.find(‘InnerComponent’).length).toBe(1);
}

However, I get errors like "Cannot read property 'state' of undefined". I understand that using shallow rendering doesn't give me the exact structure that I need, but I'm not sure what else to try to get the right structure. I tried shallow-rendering the wrapped component and then finding the unwrapped component within it, like so, component = shallow(<MyComponent {...props} />).find('MyComponent').shallow();, with no luck. Any other suggestions would be appreciated. `

like image 723
user3802348 Avatar asked Jan 30 '18 06:01

user3802348


People also ask

Which is better Enzyme or Jest?

Many people choose to use Jest and Enzyme together to test their React web applications. They use Jest as a test runner and assertion library, then use Enzyme to build the tests for their UI. This results in slimmer, cleaner testing code that's also easier to debug when a test breaks.

How to write a jest test using NPM?

To start writing tests in Jest, we first need to get the dependency. Run npm i jest to install it. Once ready, we can add a script to our package.json file which will run Jest for us: Now every time we run npm run unit, it will call the run.js file which will execute Jest. We can also specify a config file.

How do I mock a hoc in jest?

The easiest way to mock HOC is to create a __mock__ directory in the same path of HOC and a mock file with the same name of with-something where we can simplify the code (with a mock data structure prop) : Finally our Jest test (in this case using @testing-library/react) will look like this:

What is a jest test?

Jest Tutorial: what is Jest? Jest is a JavaScript test runner, that is, a JavaScript library for creating, running, and structuring tests. Jest ships as an NPM package, you can install it in any JavaScript project. Jest is one of the most popular test runner these days, and the default choice for React projects.

How do I set up a jest project?

As with every JavaScript project you'll need an NPM environment (make sure to have Node installed on your system). Create a new folder and initialize the project with: Next up install Jest with: Let's also configure an NPM script for running our tests from the command line. Open up package.json and configure a script named test for running Jest:


1 Answers

Usually you want to test the component and not the integration of the component with redux:

class MyComponent extends Component {
    //code here
}

export { MyComponent } // export the component
export default compose(
    connect(mapStateToProps),
)(MyComponent)

Also on your test you would import the named export import { MyComponent } from '...' instead of importing the default: import MyComponent from '..'

import { MyComponent } from ‘app/MyComponent’;

describe(‘<MyComponent />’, () => {
  let component;
  beforeEach(() => {
    props = {
      id: ‘23423’     
    }
    component = shallow(<MyComponent {…props} />);
  }


  it(‘Loads correctly’, () => {
    expect(component.state(‘isLoading’)).toEqual(true);
    expect(component.find(‘InnerComponent’).length).toBe(1);
  }
}

If you want to test component interactions with your redux store you need to wrap your component with a <Provider />

import { MyComponent } from ‘app/MyComponent’;
import { Provider } from 'react-redux'

    describe(‘<MyComponent />’, () => {
      let component;
      beforeEach(() => {
        props = {
          id: ‘23423’     
        }
        component = shallow(<Provider><MyComponent {…props} /></Provider>);
      }

You should definitely read the testing section of the redux documentation

like image 139
Fabio Antunes Avatar answered Oct 12 '22 23:10

Fabio Antunes