Configuring JSDOM with Mocha

I am trying to use synon mock and spy to test Redux components and asynchronous actions, but as soon as I import sinon into any test file, run the following npm script:

mocha --require test / helpers / browser.js --compilers .: babel-core / register --opts test / client / ** / *. {js, jsx} - recursive test / client

I get the following error:

var div = typeof document !== "undefined" && document.createElement("div");
                                                      ^

TypeError: document.createElement is not a function in ... / node_modules / sinon / lib / sinon / util / core / deep-equal.js: 3: 55

browser.js is where I set up the JSDOM:

import { JSDOM } from 'jsdom';

const doc = new JSDOM('<!DOCTYPE html><html><head></head><body></body></html>');
cost win = doc.defaultView; // tried doc.window;

global.document = doc;
global.window = win;

/*
Object.keys(win).forEach(property => {
  if (typeof global[property] === 'undefined') {
    global[property] = win[property];
  }
});
*/

global.navigator = {
  userAgent: 'node.js'
};

I suppose I don't have the correct jsdom setting? I tried to look back and found the commented-out code in the browser.js file above, but it causes an error when uncommenting:

Object.keys(win).forEach(function (property) {
       ^

TypeError: Cannot convert undefined or null to an object.

+4
1

document window, :

const dom = new JSDOM('<!DOCTYPE html><html><head></head><body></body></html>');

global.window = dom.window;
global.document = dom.window.document;
+16

Source: https://habr.com/ru/post/1677097/


All Articles