I am struggling to get mocha, jsdom, es6 modules with babel to play nice with jquery.
Here's a module
// lib/dom.js
import $ from 'jquery';
const node = (tag)=> $(`<${tag} />`)
export {
node
}
Here's a test
// test/dom.js
import assert from 'assert';
import { node } from '../src/lib/dom';
describe('node(tag)', ()=> {
it('should return a Node', ()=> {
let img = node('img');
assert(img.nodeName === 'IMG');
});
});
Here's the jsdom setup
// test/_setup.js
var jsdom = require('jsdom');
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.parentWindow;
The script to run
mocha --compilers js:babel-register --recursive
What do I need to change to allow jsdom to load jquery so that dom.js can load the jquery module without getting the error:
Error: jQuery requires a window with a document
Here's the full source https://github.com/markbrown4/test-simple
Got it, test/_setup.js needed the following:
global.document = require('jsdom').jsdom('<html></html>');
global.window = document.defaultView;
global.$ = require('jquery')(window);
document.parentWindow is deprecated from jsdom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With