I'm trying to write the first test for React components and keep getting error:
ReferenceError: navigator is not defined
I have component and one of it's children use codemirror for displaying editable textareas. The thing is that in codemirror there is a check for type of navigator. And since I run this code not in browser, but in terminal with node.js it's not defined.
Some folks on SO advised to set global variable, but it didn't work for me. Here is code of the test:
global.navigator = {
userAgent: 'node.js'
};
import React from 'react'
import { shallow, render } from 'enzyme'
import { expect } from 'chai'
import { MessagesView } from '../../components/MessagesView'
describe('components', () => {
describe('Message views', () => {
it('render buttons', () => {
})
})
})
Is there still a way to set navigator variable? Or maybe I can set global variables with mocha options?
Take a look here https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md
Basically you need to configure jsdom to create the window object for you.
This should be placed in the setup file of Mocha.