question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customize "files" option in Gruntfile

See original GitHub issue

Hi there,

I wanted to override the “files” parameters of my karma.conf.js inside grunt-karma plugin. My use case is having a list of common files (like all AngularJS files) and adding either underscore or lodash to test that the library is compatible with both third part libraries.

My first idea was something like:

karma: {
  options: {
    configFile: 'karma.conf.js',
    browsers: ['PhantomJS']
  },
  buildLodash: {
    files: [
       JASMINE,
       JASMINE_ADAPTER,
      'http://code.angularjs.org/1.1.4/angular.js',
      'http://code.angularjs.org/1.1.4/angular-resource.js',
      'http://code.angularjs.org/1.1.4/angular-mocks.js',
      'http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.1.1/lodash.js',
      'src/*.js',
      'test/*.js'
      'src/*.js',
      'test/*.js'
    ],
    singleRun: true
  },
  buildUnderscore: {
    files: [
       JASMINE,
       JASMINE_ADAPTER,
      'http://code.angularjs.org/1.1.4/angular.js',
      'http://code.angularjs.org/1.1.4/angular-resource.js',
      'http://code.angularjs.org/1.1.4/angular-mocks.js',
      'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore.js',
      'src/*.js',
      'test/*.js'
    ],
    singleRun: true
  }
}

But that didn’t work because JASMINE is not defined… fair enough. Is there a way to define both JASMINE and JASMINE_ADAPTER? I saw there was defined inside grunt-karma/node_modules/karma/lib/config.js but I have no idea how to import them…

As an alternative, what would think about a way to extend the arrays from the config file rather than only overriding them? Something like filesExtend, exludeExtend, reportersExtend and browersExtend options which would apply a concat between the array from the karma.conf.js file and the Gruntfile. It could be extend a second time inside tasks. Example:

karma.conf.js

files = [
  JASMINE,
  JASMINE_ADAPTER
]

Gruntfile.js

karma: {
  options: {
    configFile: 'karma.conf.js',
    browsers: ['PhantomJS'],
    filesExtend: [
      'http://code.angularjs.org/1.1.4/angular.js',
      'http://code.angularjs.org/1.1.4/angular-resource.js',
      'http://code.angularjs.org/1.1.4/angular-mocks.js',
      'src/*.js',
      'test/*.js'
    ]
  },
  buildLodash: {
    filesExtend: [
      'http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.1.1/lodash.js'
    ],
    singleRun: true
  },
  buildUnderscore: {
    filesExtend: [
      'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore.js'
    ],
    singleRun: true
  }
}

This would produce the expected result : both builds would have all AngularJS and JASMINE files, and either lodash or underscore depending on the task.

What do you think?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
leachryanbcommented, Oct 31, 2013

I have a workaround for this with grunt. Set the files in the default options, then override via grunt.config in your task call:

karma: {
    options: {
        basePath: 'src',
        configFile: 'karma.conf.js',
        // browsers: ['Chrome']
        files: [
            './vendor/requirejs/require.js',
            './config.js',
            './spec/main-test.js',
            { pattern: '**/*.*', included: false },
            { pattern: '*.*', included: false }
        ]
    },
    unit: {
        autoWatch: true
    },
    ci: {
        basePath: 'test',
        autoWatch: false,
        singleRun: true
    }
}

grunt.registerTask('test:ci', function() {
    grunt.task.run('testprepare');
    grunt.task.run('buzz_build:test');
    grunt.config('karma.options.files', [
        './buzz.js',
        './spec/main-test.js',
        { pattern: '**/*.*', included: false },
        { pattern: '*.*', included: false }
    ]);
    grunt.task.run('karma:ci');
});
1reaction
gantaacommented, Aug 27, 2013

Thanks to @geddski for this: It turns out this pull request isn’t needed. if you wrap “files” in options, everything plays nice. Here is an example:

var myModuleFiles = [(array of paths)]
var libraryFiles = [(array of more paths)]
grunt.initConfig({
    karma: {
        options: {
            configFile: '/build/test/karma.conf.js'
            , browsers: ['Chrome']
        }
        , nonreactor: {
            options: {
                files: myModuleFiles
            }
        }
        , reactor: {
            options: {
                files: myModuleFiles.concat(libraryFiles)
            }
        }
    }
});

Thankfully, the global karma options (configFile and browsers) are not blown away when specifying the options object again in each karma runner execution config.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring tasks - Grunt: The JavaScript Task Runner
Custom Filter Function ... The filter property can help you target files with a greater level of detail. Simply use a valid fs.Stats...
Read more >
Programmatically set options for grunt task?
Create a new task which set the option, then call the modified task. This is a real life example with assemble: grunt.registerTask('build_prod', 'Build...
Read more >
Organizing Your Grunt Tasks
Let's create a directory named grunt and add the task options for cssmin by creating a file named `cssmin.js`. Your project's directory ...
Read more >
Getting started with Grunt.js - Semaphore Tutorial
Install Node.js and Grunt. · Create package.json and list dependencies (Grunt and plugins). · Install NPM modules. · Create Gruntfile.js .
Read more >
11 Optimize Your Builds and Audit Your Code Using Grunt
You can use Grunt to build your application from the sources stored in your Git repository or stored locally. Your visual application includes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found