Larson S Larson S - 4 months ago 19x
Javascript Question

Continue certain tasks in grunt even if one fails

Is there a way to configure a sequence of tasks so that specific subsequent ones (I don't want --force on the whole batch) run even if one fails? For example, consider a case like this

  1. Create some temporary files

  2. Run some unit tests which involve those temporary files

  3. Clean up those temporary files

I can do this:

grunt.registerTask('testTheTemp', ['makeTempFiles', 'qunit', 'removeTempFiles']);

But if qunit fails then the removeTempFiles task never runs.


Here's one workaround. It's not pretty, but it does solve the issue.

You create two extra tasks which you can wrap at the beginning/end of any sequence that you want to continue even over failure. The check for existing value of grunt.option('force') is so that you do not overwrite any --force passed from the command line.

 'force the force option on if needed', 
 function() {
  if ( !grunt.option( 'force' ) ) {
    grunt.config.set('usetheforce_set', true);
    grunt.option( 'force', true );
  'turn force option off if we have previously set it', 
  function() {
  if ( grunt.config.get('usetheforce_set') ) {
    grunt.option( 'force', false );
grunt.registerTask( 'myspecialsequence',  [
] );

I've also submitted a feature request for Grunt to support this natively.