bier hier bier hier -4 years ago 122
Javascript Question

How to mock object in mocha unittest?

In my reactjs component I have a method called getContainer(doc) with this line:

let w = Math.max(doc.documentElement.clientWidth, window.innerWidth);

It takes 1 parameter called doc: htmldocument. In my mocha unittest I got:

let mockDoc = {documentElement: {clientWidth:100}}
let res = wrapper.instance().getContainer(mockDoc);

When I run the test I still get an error , how can I fix this?


TypeError: Cannot read property 'clientWidth' of undefined

Answer Source

you can use an visual dom library like jsdom,for more details you can see is simple to is a demo by enzyme

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download