Karma: Setup globals via karma.conf.js

Created on 3 Dec 2015  路  5Comments  路  Source: karma-runner/karma

It would be helpful to allow karma to setup globals in karma.conf.js via a globals object.
This would be, for instance, helpful with tests that run agains an API (like contract tests).

If the configuration is kept in a json file (common scenario), it currently requires a trickery like this:

var fs = require('fs');
var MY_GLOBAL_API = 'some global value';
fs.writeFileSync('globals.js'), 'apiEndpoint = "' + MY_GLOBAL_API + '";', 'utf8');

module.exports = function(config) {

  var configuration = {
    files: [
        'globals.js'
        .....

It would have been a bit easier with:

var MY_GLOBAL_API = 'some global value';
module.exports = function(config) {

  var configuration = {
    globals.apiEndpoint = MY_GLOBAL_API;
    .....

Sort of similar to how protractor does it. I'd be happy to work on a PR.

Most helpful comment

I think this could be a plugin rather than core functionality. It would have a config very similar to what you suggesed:

module.exports = function (config) {
  config.set({
    globals: {
      myGlobal: 42
    }
  })
}

and would under the hood just generate a globals file and inject it into the files array.

All 5 comments

+1 I would like this as well, since my global variables are stored in a node script for use by a Grunt task that generates the HTML template with the global variables for my app.

+1

I think this could be a plugin rather than core functionality. It would have a config very similar to what you suggesed:

module.exports = function (config) {
  config.set({
    globals: {
      myGlobal: 42
    }
  })
}

and would under the hood just generate a globals file and inject it into the files array.

Does anyone has solution of this issue since 2016 ?

Ran into this issue while trying to set up contact testing (Pact) and. I cannot seem to require Pact inside the test file.

Was this page helpful?
0 / 5 - 0 ratings