简体   繁体   English

如何从Grunt对节点模块执行茉莉测试

[英]How to execute jasmine tests for node modules from grunt

I want to run some Jasmine 2.x tests for node.js modules in a Grunt build. 我想在Grunt版本中为node.js模块运行一些Jasmine 2.x测试。 My setup looks like this: 我的设置如下所示:

src/foo.js SRC / foo.js

exports.bar = 23;

spec/foo.spec.js 投机/ foo.spec.js

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

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

With grunt-contrib-jasmine the node module system is not available and I get grunt-contrib-jasmine节点模块系统不可用,我得到

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

There is grunt-jasmine-node , but it depends on jasmine-node which is unmaintained and includes Jasmine 1.3.1, so this is not an option. grunt-jasmine-node ,但是它取决于未维护的jasmine-node ,包括Jasmine 1.3.1,因此这不是一个选择。

Jasmine supports node.js out of the box , by including a file jasmine.json in the spec directory, I can run the tests with the jasmine cli. Jasmine 开箱即 jasmine.json 支持node.js ,通过在spec目录中包含文件jasmine.json ,我可以使用jasmine cli运行测试。 Is there any clean way to run the same tests from grunt as well? 是否有任何干净的方法也可以通过grunt运行相同的测试?

You could use grunt-exec , which just executes the value as if typed on the command line: 您可以使用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"
    ]);
};

This will allow you to keep jasmine up to date as well as use it with other grunt tasks. 这将使您可以使茉莉花保持最新状态,并与其他艰苦的任务一起使用。

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

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