grunt-run-task

Run Grunt tasks in you tests, so you can test them

grunt-run-task

Run Grunt tasks in you tests, so you can test them.

So you want to test your Grunt tasks, eh? And you don't want to litter your project’s Gruntfile with tasks that are needed “just for the tests”? And you want your tests to be self-contained? Then this module is for you!

This package gives you the tools to run Grunt tasks inside your tests and clean up afterwards. You can use your test tool’s setup and tear down methods to run the Grunt task you want to test with arbitrary configuration.

If you’re using BDD it could look like this:

var assert = require('assert');
var grunt = require('grunt');
var runTask = require('grunt-run-task');
 
describe('my awesome Grunt task', function () {
  runTask.loadTasks('tasks');
 
  describe('when using the default options', function () {
    var task = runTask.task('awesome_task', {
      options: {},
      src: [ 'test/fixtures/src.js' ],
      dest: 'tmp/src.out.js'
    });
 
    beforeEach(task.run());
    afterEach(task.clean());
 
    it('does awesome stuff', function() {
      assert(grunt.file.exists(task.files[0].dest));
    });
  });
});

This module is tested with Grunt 0.4.x and Node.js 0.8.x, 0.10.x and 0.11.x. Use npm to install it:

npm install --save-dev grunt-run-task

Run the given task immediately with the given config. Call the callback, once the task has finished. Returns the created Task instance.

  • name String The name of the task to run. This may include colon-separated arguments to pass to the task, or, in the case of multi-tasks, the name of the target to run.
  • config Object The configuration to use for the task.

Run the jshint task.

runTask.loadNpmTasks('grunt-contrib-jshint');
runTask('jshint:default', {
  // JSHint is a multi-task, setup the "default" target 
  default: {
    files: [ '**/*.js' ]
  }
}, function (errtask) {
  if (err) {
    // The task did encounter an error 
  }
});

Return a new Task object that can be used to run the given task.

  • name String The name of the task to run. This may include colon-separated arguments to pass to the task, or, in the case of multi-tasks, the name of the target to run.
  • config Object The configuration to use for the task.

Run the uglify task, and cleanup created files afterwards.

runTask.loadNpmTasks('grunt-contrib-uglify');
var task = runTask.task('uglify', {
  default: {
    files: {
      'build/app.js': [ '**/*.js' ]
    }
  }
});
 
task.run('default', function (err) {
  task.clean(function (err) {
    // Done 
  });
});

Wrapper for the grunt.initConfig method.

Wrapper for the grunt.registerTask method.

Wrapper for the grunt.registerMultiTask method.

Wrapper for the grunt.registerInitTask method.

Wrapper for the grunt.renameTask method.

Wrapper for the grunt.loadTasks method.

Wrapper for the grunt.loadTasks method.

Wrapper for the grunt.option method.

The task class implements the EventEmitter2 API. Any events that are emitted by Grunt while your task runs are forwarded to the task class.

  • name String The name of the task.
  • multi Boolean Whether this is a multi-task.
  • init Boolean Whether this is a init-task
  • target String If the task is a multi-task, this is the target name it was created with.
  • args Array Any arguments that were passed when creating the task.
  • files Array The destination files that this task (might have) created.

Run the task with the given arguments. If the last argument is a function, run the task immediately and call the function once the task finished. Otherwise, return a function expecting a single parameter, the callback, that actually runs the task.

Options

  • arguments String Any arguments to pass to the task, similar to what you would specify on the command line, by appending colon-separated options to the task you wish to run.
  • done Function A callback to be called once the task has finished.

Run the task with the given arguments, but expect it to fail. If the last argument is a function, run the task immediately and call the function once the task finished. Otherwise, return a function expecting a single parameter, the callback, that actually runs the task.

Options

  • arguments String Any arguments to pass to the task, similar to what you would specify on the command line, by appending colon-separated options to the task you wish to run.
  • done Function A callback to be called once the task has finished.

Clean up the task's dest files.

Options

  • done Function A callback to be called once all files have been removed.

License

Copyright © 2014 Jonas Pommerening

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.