lundi 7 janvier 2019

How to test React component with mount method of enzyme

I'm trying to use the mount method of the enzyme library to render a custom component but it fails with the error

Invariant Violation: Tester.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

together with some annoying stack traces.

Here is the code snippet

import React from 'react'
import {shallow, mount, render} from 'enzyme'


describe('Dashboard', () => {
    let testComp;
    beforeEach(()=>{
        testComp = mount(  <UserDashboard />  )
    });

    it('should render self without breaking.', () => {
        expect(testComp).not.toBe(null)
    })

})

But I noticed that the problem is not with the UserDashboard component because I still get the same error when I replace it with a simple div element.

testComp = mount(  <div />  )

The shallow and render methods work pretty well but the mount method is being really stubborn.

Please I'm new to testing react components and any help on why this is happening will be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire