whois42
whois42

Reputation: 186

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?

Upvotes: 2

Views: 947

Answers (1)

Pierre Criulanscy
Pierre Criulanscy

Reputation: 8686

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

Upvotes: 4

Related Questions