[英]$ is not defined in mocha chai test script
i am trying to write a unit test case for a js function, its inside a .js file am loading that to a test.js file and then run我正在尝试为一个 js 函数编写一个单元测试用例,它在一个 .js 文件中,我正在将它加载到一个 test.js 文件中,然后运行
npm test test.js npm 测试 test.js
my test script contains我的测试脚本包含
const app = require('../js/customJs/myjs.js');
var assert = require('chai').assert;
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
describe('Testing my test functions', function () {
console.log("ready 2 test d dom");
const dom = new JSDOM('<!DOCTYPE html><p id="myid"></div>');
it('check the return value of helloworld module', function () {
assert.equal(dashboard_tab.helloworld(),"hello");
});
});
'myjs.js' file has helloworld function which gets a value from a dom element and call an ajax call , so i am trying to unit test if the return value is "hello"('of course this test fails, since that function will not be returning hello'),but while running this test script i was getting error like '$ is not defined' 'myjs.js' 文件有一个 helloworld 函数,它从一个 dom 元素获取一个值并调用一个 ajax 调用,所以我试图单元测试返回值是否为“hello”('当然这个测试失败了,因为那个函数会不是返回 hello'),但是在运行此测试脚本时,我收到了“$ 未定义”之类的错误
myjs.js myjs.js
var count = 0;
function helloworld(){
var id_Val = $("#myid").val(); //i was getting $ is not defined here when i run test.js
$.ajax({
url: "test/",
data: { 'id': id_Val },
dataType: 'json',
success: function (data) {
//some code
}
});
}
i tried我试过
global.$ = require('jquery')(window); global.$ = require('jquery')(window); // now, window is not defined // 现在,窗口未定义
how to unit test a js function which includes jquery ($)如何对包含 jquery ($) 的 js 函数进行单元测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.