【问题标题】:How to execute jasmine tests for node modules from grunt如何从 grunt 为节点模块执行 jasmine 测试
【发布时间】:2015-05-20 09:43:02
【问题描述】:

我想在 Grunt 构建中为 node.js 模块运行一些 Jasmine 2.x 测试。我的设置如下所示:

src/foo.js

exports.bar = 23;

spec/foo.spec.js

var foo = require("../src/foo.js");

define("foo", function() {
  it("exports bar as 23", function() {
    expect(foo.bar).toBe(23);
  });
});

grunt-contrib-jasmine 节点模块系统不可用,我得到了

>> ReferenceError: Can't find variable: require at
>> spec/foo.spec.js:1

grunt-jasmine-node,但它依赖于未维护的jasmine-node,包括Jasmine 1.3.1,所以这不是一个选项。

Jasmine supports node.js out of the box,通过在spec 目录中包含一个文件jasmine.json,我可以使用jasmine cli 运行测试。是否有任何 clean 方法可以从 grunt 运行相同的测试?

【问题讨论】:

    标签: javascript node.js gruntjs jasmine


    【解决方案1】:

    您可以使用grunt-exec,它只是执行值,就像在命令行上键入一样:

    module.exports = function (grunt) {
    
        grunt.initConfig({
    
            exec: {
                jasmine: "jasmine"
            },
    
            env: {
                test: {
                    NODE_ENV: "test"
                }
            }
        });
    
        grunt.loadNpmTasks("grunt-exec");
        grunt.loadNpmTasks("grunt-env");
    
        grunt.registerTask("test", [
            "env:test",
            "exec:jasmine"
        ]);
    };
    

    这将使您可以使 jasmine 保持最新状态,并将其与其他 grunt 任务一起使用。

    【讨论】:

    • 完美,可行。当测试失败时,jasmine 进程退出并返回一个错误代码,进而导致 grunt 构建失败。
    猜你喜欢
    • 1970-01-01
    • 2013-12-16
    • 2018-10-16
    • 2014-12-14
    • 2015-07-25
    • 2015-08-26
    • 2016-03-08
    • 2015-07-19
    • 2014-09-02
    相关资源
    最近更新 更多