对前端js代码添加测试:Error: jQuery requires a window with a document
来源:互联网 发布:传奇世界数据封包 编辑:程序博客网 时间:2024/05/04 05:32
在对前端的js代码添加测试的时候,因为项目需要,首先把amd格式的代码改成了cmd。但是在引用index.js 进行测试的时候,一直报 Error: jQuery requires a window with a document 错误。查阅发现,较新版本的jquery里没有jsdom,所以在node中调用jquery的时候,会报 Error: jQuery requires a window with a document(jquery需要document对象)。
解决办法:(使用 jsdom
mock document);
安装
jsdom
包npm install --save-dev jsdom
引用
jsdom
const jsdom = require('jsdom');let $ = null, index = null;jsdom.env( "http://www.baidu.com", function (err, window) { $ = require('jquery')(window); index = require('../../somepage/index.js'); });
接下来就可以对 index.js 文件的函数添加测试啦。
- 还有一些其他的解决办法,可以去这里看一下呀~ http://quaintous.com/2015/07/31/jqery-node-mystery/
0 0
- 对前端js代码添加测试:Error: jQuery requires a window with a document
- jQuery requires a window with a document
- Node jquery requires a window with a document
- jQuery requires a window with a document异常解决方案
- [node]jQuery requires a window with a document
- nodejs中使用jquery出现“jQuery requires a window with a document”错误的解决方案
- js中window.onload 与 jquery中$(document.ready()) 测试
- window.opener.document.getElementById(“a”)用标准的Jquery 语言怎么写
- Clear a form with jQuery
- struct.error: unpack requires a string argument of length 4
- fatal error C1190: managed targeted code requires a '/clr' option
- JQuery window、document、 body
- JQuery window、document、 body
- ate a window with a bit depth of 32
- jQuery中的$(document).ready()与js中的window.onload
- js的window.onload和jquery document.ready()的区别
- 对JS中global、window、document、this区别的理解
- JS: document,window
- MVC之简单控制器实现与原理
- 【传感器】AT24C128 EEPROM
- 命令行下操作MySQL数据库,各种命令的使用入门示例。(附图)
- weblogic10下部署润乾报表demo
- 写操作系统----4.开启分页
- 对前端js代码添加测试:Error: jQuery requires a window with a document
- 写操作系统----5.解决读取kernel跨段
- mybatis入门
- 文件访问权限问题:Please make sure it is a directory writable by the Web server process
- Fiddler(二) - 使用Fiddler做抓包分析
- 使用 Smartmontools 检测硬盘坏道
- 使用 supervisor 管理进程
- 如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境
- C程序设计语言练习1-16 修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多的打印文本