如何测试使用jsdom导入jQuery的ES6模块

马克布朗4

我正在努力通过babel获得mocha,jsdom,es6模块,以便与jquery配合使用。

这是一个模块

// lib/dom.js
import $ from 'jquery';

const node = (tag)=> $(`<${tag} />`)

export {
  node
}

这是一个测试

// 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');
  });
});

这是jsdom设置

// test/_setup.js
var jsdom = require('jsdom');

global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.parentWindow;

要运行的脚本

mocha --compilers js:babel-register --recursive

我需要更改什么以允许jsdom加载jquery,以便dom.js可以加载jquery模块而不会出现错误:

错误:jQuery需要带有文档的窗口

这是完整的源https://github.com/markbrown4/test-simple

马克布朗4

知道了,test / _setup.js需要以下内容:

global.document = require('jsdom').jsdom('<html></html>');
global.window = document.defaultView;
global.$ = require('jquery')(window);

document.parentWindow已从jsdom中弃用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章