Error: It looks like you called 'mount ()' without a global document being loaded

1

I get this error when I run my tests.

import { Meteor } from 'meteor/meteor';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-addons-test-utils';
import { mount } from 'enzyme';
import { chai,  assert } from 'meteor/practicalmeteor:chai';
import { Factory } from 'meteor/dburles:factory';
import faker from 'faker';
import StubCollections from 'meteor/hwillson:stub-collections';


import {Button, FormGroup, ControlLabel, FormControl, HelpBlock, 
        ButtonToolbar, Checkbox, Radio, Glyphicon} from 'react-bootstrap';

import MainApp  from '/imports/ui/MainApp.jsx';
import {Companies} from '/imports/api/companies.js';

import {
  insert,
} from '/imports/api/companies.js';




describe('Server', () => {


    Factory.define('user', Companies, {
      firstName: () => 'Daniel',
      lastName: () => 'Peña',
      orgType: () => 'non-profit',
    });

    if (Meteor.isServer) 

        it('Se insertan los datos', () => {

            const users = Factory.create('user');
            StubCollections.stub(Companies);
            Companies.insert({ users });
            assert.equal(Companies.find().count(), 1);
            StubCollections.restore();
        });

        it('No se insertan los datos al no seleccionar el TOS', () => {
            const main = mount(<MainApp/>);

            const users = Factory.create('user');
            StubCollections.stub(Companies);

            main.find('div.checkbox').simulate('change', {
              target: { checked: false }
            });

            Companies.insert({ users });
            assert.equal(Companies.find().count(), 0);
            StubCollections.restore();
        });

    return;


});
    
asked by Daniel Peña 11.10.2016 в 20:49
source

1 answer

0

This error can occur when you are in an environment without a browser. According to the enzyme documentation (my translation):

  

Because the enzyme mount API requires a DOM, JSDOM is required in order to use mount if you are not yet in a browser environment (for example, a Node environment).

On the airbnb github there are a couple of bugs similar that receive the same error message you indicate. In both cases they solved it by importing JSDOM and creating an "empty DOM". The code is something like this (copied from the first link):

import jsdom from 'jsdom'
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.document = doc
global.window = doc.defaultView
    
answered by 12.10.2016 / 20:36
source