簡體   English   中英

$ 未在 mocha chai 測試腳本中定義

[英]$ is not defined in mocha chai test script

我正在嘗試為一個 js 函數編寫一個單元測試用例,它在一個 .js 文件中,我正在將它加載到一個 test.js 文件中,然后運行

npm 測試 test.js

我的測試腳本包含

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' 文件有一個 helloworld 函數,它從一個 dom 元素獲取一個值並調用一個 ajax 調用,所以我試圖單元測試返回值是否為“hello”('當然這個測試失敗了,因為那個函數會不是返回 hello'),但是在運行此測試腳本時,我收到了“$ 未定義”之類的錯誤

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

我試過

global.$ = require('jquery')(window); // 現在,窗口未定義

如何對包含 jquery ($) 的 js 函數進行單元測試

這可能是由 $ 沖突引起的

請點擊此處了解如何避免。

當您多次加載 jQuery 文件時,也會發生 $conflict。 你能不能也檢查一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM