【问题标题】:How to test client-side code with NodeUnit如何使用 NodeUnit 测试客户端代码
【发布时间】:2012-06-25 22:11:54
【问题描述】:

这是my previous question的后续行动。

假设我正在编写带有calculate 函数的calculate.js 源文件以在我的客户端代码中使用它:

函数计算(num){
    返回数 * 2;
}

现在我想测试一下。

我正在使用nodeunit 中描述的this article导出 calculate 函数(以便测试可以调用它)。

函数计算(数){ 返回数 * 2; } export.calculate = 计算 // Node.js 的东西。它不会在浏览器中运行!

nodeunit 测试运行正常,但我不能再在我的 Web 应用程序中使用 calculate.js,因为它使用了 exports

现在它看起来像一个陷阱。我可以解决这个问题吗?

【问题讨论】:

标签: javascript unit-testing node.js client-side


【解决方案1】:

使用非 Node.js 测试框架,如 jasminebdd 或 jstestdriver。

【讨论】:

  • 谢谢。我仍然想通过node 运行我的测试。 AFAIR jstestdriver 是 Java,所以我想避免它。我可以让node 运行jamsine 吗?
  • 阅读此内容可能会将无节点 js 加载到节点 js stackoverflow.com/questions/5171213/…
  • 谢谢。也许解决方案只是制作“节点模块包装器”。
  • 只是想知道为什么你需要通过节点运行它?当它只是简单的 js 时
  • Node 看起来像是在命令行中运行 java 脚本的便捷工具。所以我正在考虑将它用于单元测试。也许这是我的错。
【解决方案2】:

Running tests in the browser

如果您希望为您的测试套件使用 CommonJS 格式(使用 导出),由您来定义 CommonJS 工具 浏览器。有很多选择,它很重要 使用您现有的代码,这就是为什么 nodeunit 目前没有 开箱即用。

我认为你应该看看 Browserify 来完成这个。

Browserify

使用服务器端构建使 Node.js 样式的 require() 在浏览器中工作 一步,仿佛施了魔法!

Mocha

Mocha 是一个简单、灵活、有趣的 Node.js JavaScript 测试框架 和浏览器。

我认为你应该看看我非常喜欢的由 TJ(Express.js 的作者)创建的 Mocha(比我过去使用的 nodeunit 更好)。使用 Mocha,您可以轻松地进行代码覆盖、监视模式、通知,而且 Mocha 的一大优点是维护得更好。

【讨论】:

    【解决方案3】:

    Karma runner 有一个 nodeunit adapter 在浏览器和 PhantomJS 中运行 nodeunit 测试。这是一个示例 test entrypointGruntfile 使用 Browserify 运行用 CommonJS modules 编写的测试(导出)。

    【讨论】:

      【解决方案4】:

      nodeunit-browser-tap + Testling 是另一个选项,如果你设置在 nodeunit 上。

      • Testling npm package 为您在本地浏览器中运行的测试提供命令行输出。
      • 如果您在 GitHub 上托管,Testling.com 会为您提供免费的跨浏览器 CI。
      • browser-tap 包装器为您提供 Testling 可以理解的 nodeunit 输出。

      【讨论】:

        【解决方案5】:

        您实际上可以在 nodeunit 本身内测试独立的 jQuery 逻辑。例如,这是我为我的 datepicker 库编写的一些测试:

        /* jshint evil:true */
        /* global module, require, DatePair */
        
        var fs = require('fs');
        // Some of the plugins use a jQuery variable instead of $
        var jQuery = require('jquery');
        var $ = jQuery;
        
        // Recreate window and document
        var window = { jQuery: jQuery };
        var document = window;
        
        // The file is included here:
        eval(fs.readFileSync('../lib/js/jquery/plugins/jquery.cookie.js') + '');
        eval(fs.readFileSync('../lib/js/jquery.timepicker/jquery.timepicker.js') + '');
        eval(fs.readFileSync('../lib/js/bootstrap/bootstrap-datepicker.js') + '');
        eval(fs.readFileSync('../homepage/js/datepair.js') + '');
        
        // Initialize DatePair Object
        var datePair = DatePair(new Date(), {
            dateStart: $('<input type="text" autocomplete="off">'),
            dateEnd: $('<input type="text" autocomplete="off">'),
            timeStart: $('<input type="text" autocomplete="off">'),
            timeEnd: $('<input type="text" autocomplete="off">')
        });
        
        module.exports = {
            'Time Delta Var Is 1 hour' : function (test) {
                test.equal(datePair.getTimeDelta(), 3600);
                test.done();
            },
            'Change Date And Check For 1 Hour Difference' : function (test) {
                datePair.startTime.val("10:00pm").change();
                test.equal(datePair.endTime.val(), "11:00pm");
                test.done();
            },
            'Make End Date Move Up One Day' : function (test) {
                var expectedEndDates = [1];
                datePair.startTime.val("11:00pm").change();
                expectedEndDates.push(parseInt(datePair.startDate.val().split("/")[1], 10) + 1);
        
                test.ok($.inArray(parseInt(datePair.endDate.val().split("/")[1], 10), expectedEndDates) !== -1);
                test.equal(datePair.endTime.val(), "12:00am");
                test.done();
            },
            'Change To 11:30pm Of Previous Day' : function (test) {
                // Move startTime 1 Day Forward
                datePair.startTime.val("11:00pm").change();
                // Move EndDate 1 Day Backwards
                datePair.endTime.val("11:30pm").change();
        
                test.equal(datePair.endDate.val(), datePair.startDate.val());
                test.done();
            }
        }
        

        在此处查看完整的代码 + 库:https://github.com/keithhackbarth/jquery-datetime-picker

        【讨论】:

          猜你喜欢
          • 2013-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多