对前端js代码添加测试:Error: jQuery requires a window with a document

在将AMD格式的前端JS代码转换为CMD并尝试进行测试时,遇到了'Error: jQuery requires a window with a document'的错误。原因是新版本的jQuery缺少jsdom,无法在Node环境中运行。解决方法是在测试中模拟document对象,通过安装相关包并引用,成功解决了问题。此外,还有其他解决方案可参考相关链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在对前端的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);

  1. 安装jsdom

    npm install --save-dev jsdom
  2. 引用 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');
        }
    );
  3. 接下来就可以对 index.js 文件的函数添加测试啦。

  4. 还有一些其他的解决办法,可以去这里看一下呀~ http://quaintous.com/2015/07/31/jqery-node-mystery/
/*! * jQuery JavaScript Library v1.12.4 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2016-05-20T17:17Z */ (function( global, factory ) { if ( typeof module === "object" && typeof module.exports === "object" ) { // For CommonJS and CommonJS-like environments where a proper `window` // is present, execute the factory and get jQuery. // For environments that do not have a `window` with a `document` // (such as Node.js), expose a factory as module.exports. // This accentuates the need for the creation of a real `window`. // e.g. var jQuery = require("jquery")(window); // See ticket #14549 for more info. module.exports = global.document ? factory( global, true ) : function( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); }; } else { factory( global ); } // Pass this if window is not defined yet }(typeof window !== "undefined" ? window : this, function( window, noGlobal ) { // Support: Firefox 18+ // Can't be in strict mode, several libs including ASP.NET trace // the stack via arguments.caller.callee and Firefox dies if // you try to trace through "use strict" call chains. (#13335) //"use strict"; var deletedIds = []; var document = window.document; var slice = deletedIds.slice; var concat = deletedIds.concat; var push = deletedIds.push; var indexOf = deletedIds.indexOf; var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty; var support = {}; var version = "1.12.4", // Define a local copy of jQuery jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery.fn.init( selector, context ); }
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值