1 votes

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

I get this error when running 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;

});

0voto

Alvaro Montoro Points 38554

This error may occur when you are in a non-browser environment. According to the enzyme documentation (my translation):

Like the enzyme API mount requires a DOM, JSDOM is necessary in order to be able 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 looks 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

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X