简体   繁体   English

使用Mocha测试JavaScript时出错

[英]Error in testing javascript with mocha

I am testing using Mocha following code. 我正在使用Mocha以下代码进行测试。 I included this javascript file in the testfile.I am trying to test function getLabels gut somehow it calls function populateTable (i am not calling it anywhere) and i get error "Uncaught TypeError: $(...).DataTable is not a function ". 我在测试文件中包含了这个javascript文件。我试图以某种方式测试函数getLabels gut调用函数populateTable(我在任何地方都没有调用它),并且出现错误“未捕获的TypeError:$(...)。DataTable不是函数” ”。 How to avoid it? 如何避免呢?

$(document).ready(function() {
    setTimeout(populateTable, 0);

    function populateTable() {
        var fetchRecords = function() {

        }
        fetchRecords();
        $('#table').DataTable({

        });
    }

    function getLabels(labels) {

    }
});

Did you include jquery in the head of your html? 您在HTML的头中是否包含了jquery?

Also, you are calling populateTable(), in this line 另外,您在这一行中调用populateTable()

 setTimeout(populateTable, 0); 

You could comment that line out and replace it getLabels(labels); 您可以注释掉该行并将其替换为getLabels(labels);。 to test that function. 测试该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM