ralfstx
ralfstx

Reputation: 4033

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. My setup looks like this:

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

With grunt-contrib-jasmine the node module system is not available and I get

>> 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.

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. Is there any clean way to run the same tests from grunt as well?

Upvotes: 1

Views: 367

Answers (1)

chrisbajorin
chrisbajorin

Reputation: 6153

You could use grunt-exec, which just executes the value as if typed on the command line:

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.

Upvotes: 2

Related Questions