Search code examples
testingreactjsmocha.jsbrowser-history

History for Mocha tests for react components


I have started to write tests for my react components with Mocha. And when I started npm test I got an exception

Invariant Violation: Browser history needs a DOM

I was tried to connect history object to test file:

 import React, {Component} from 'react'
 import Modal from 'react-modal';
 import expect from 'expect'
 import TestUtils from 'react-addons-test-utils'
 import {AdminPage} from '../../app/DOM/pages/AdminPanel'
 const history = require('history');
 const historyObj = history.createMemoryHistory();
 historyObj.createLocation('/admin');

But I'v got this exception again. How I can solve this problem?


Solution

  • As stated by the error, historyBrowser needs a DOM to work. You can simulate a DOM with a lib like jsdom to do so. Take a look to this tutorial for testing React components with mocha and jsdom