Karma: Correct inline plugin example?

Created on 18 Nov 2014  路  9Comments  路  Source: karma-runner/karma

On the Plugins page of the documentation, the following example is presented:

plugins: [
  // Karma will require() these plugins
  'karma-jasmine',
  'karma-chrome-launcher'

  // inlined plugins
  {'framework:xyz', ['factory', factoryFn]},
  require('./plugin-required-from-config')
]

Although I haven't needed to write my own inline plugins yet, I doubt the example provided is correct, since it's not even valid JavaScript syntax. Should it be { 'framework:xyz' : [ 'factory', factoryFn ] } instead? A little explanation on that point would be appreciated.

released backlog

Most helpful comment

I don't know if this is useful, but it is my local karma override file with a custom plugin that runs my delcom build light.

var exec = require('child_process').exec;

var DelcomReporter = function(helper, logger) {
    console.log('i am a delcom reporter');
    var log = logger.create('reporter.delcom');

    function delcom(colour) {
        log.info('setting delcom to ', colour);
        exec('/usr/local/bin/delcom-stoplight ' + colour, function(error, stdout, stdin) {
        }).unref();
    }

    this.adapters = [];
    this.browserCount = 0;
    this.buildOk = false;
    this.onRunStart = function(browsers) {
        this.browserCount = browsers.length;
        this.buildOk = true;
        delcom('blue');
    };

    this.onBrowserComplete = function(browser) {
        var results = browser.lastResult;
        if (results.disconnected || results.error || results.failed) {
            this.buildOk = false;
        }
    };

    this.onRunComplete = function() {
        delcom(this.buildOk ? 'green' : 'red');
    };
};

DelcomReporter.$inject = ['helper', 'logger'];

config.set({
      //browsers: ['Chrome', 'Firefox', 'Safari']
      browsers: ['Chrome'],
    plugins: [
              // Karma will require() these plugins
              'karma-jasmine',
              'karma-chrome-launcher',
                'karma-ng-html2js-preprocessor',

              // inlined plugins
              {
                    'reporter:delcom': ['type', DelcomReporter]
                }
       ],
    reporters: ['progress', 'delcom']
    });

All 9 comments

Happy to submit a PR if my assumption is verified... I'll also play with inline plugins a little on my own.

I don't know if this is useful, but it is my local karma override file with a custom plugin that runs my delcom build light.

var exec = require('child_process').exec;

var DelcomReporter = function(helper, logger) {
    console.log('i am a delcom reporter');
    var log = logger.create('reporter.delcom');

    function delcom(colour) {
        log.info('setting delcom to ', colour);
        exec('/usr/local/bin/delcom-stoplight ' + colour, function(error, stdout, stdin) {
        }).unref();
    }

    this.adapters = [];
    this.browserCount = 0;
    this.buildOk = false;
    this.onRunStart = function(browsers) {
        this.browserCount = browsers.length;
        this.buildOk = true;
        delcom('blue');
    };

    this.onBrowserComplete = function(browser) {
        var results = browser.lastResult;
        if (results.disconnected || results.error || results.failed) {
            this.buildOk = false;
        }
    };

    this.onRunComplete = function() {
        delcom(this.buildOk ? 'green' : 'red');
    };
};

DelcomReporter.$inject = ['helper', 'logger'];

config.set({
      //browsers: ['Chrome', 'Firefox', 'Safari']
      browsers: ['Chrome'],
    plugins: [
              // Karma will require() these plugins
              'karma-jasmine',
              'karma-chrome-launcher',
                'karma-ng-html2js-preprocessor',

              // inlined plugins
              {
                    'reporter:delcom': ['type', DelcomReporter]
                }
       ],
    reporters: ['progress', 'delcom']
    });

I have tried to do this recently and can confirm that it doesn't work.

With the following simple configuration

frameworks: [
  {
    'framework:xyz': ['factory', function (files, extraConfig) {
      console.log(files);
    }]
  },
  'sinon',
  'chai'
],

I get the following error message

Error: No provider for "framework:[object Object]"! (Resolving: framework:[object Object])
    at error (/Users/steve/Documents/Code/project/node_modules/di/lib/injector.js:22:12)
    at Object.get (/Users/steve/Documents/Code/project/node_modules/di/lib/injector.js:9:13)
    at Injector.get (/Users/steve/Documents/Code/project/node_modules/di/lib/injector.js:54:19)
    at /Users/steve/Documents/Code/project/node_modules/karma/lib/server.js:144:20
    at Array.forEach (native)
    at Server._start (/Users/steve/Documents/Code/project/node_modules/karma/lib/server.js:142:21)
    at Injector.invoke (/Users/steve/Documents/Code/project/node_modules/di/lib/injector.js:75:15)
    at Server.start (/Users/steve/Documents/Code/project/node_modules/karma/lib/server.js:103:18)
    at Object.exports.run (/Users/steve/Documents/Code/project/node_modules/karma/lib/cli.js:280:26)
    at requireCliAndRun (/usr/local/lib/node_modules/karma-cli/bin/karma:44:16)

It appears that the di injector is trying to fetch the factory from the known set of node_modules only (those that start with karma-) and doesn't know that this is its own function.

I'm using Karma 1.3.0

@steveworkman it's supposed to be in plugins not frameworks

@steveworkman Unfortunately documentation isn't helpful in this case.

After few hours of investigating the same thing, it seems that "low-level" config options like "frameworks", "preprocessors", "launchers" and "reporters" are used only to declare that you want to use this given tool.

However, to be able to use for example "xyz" in frameworks section, you need to register this framework in "plugins". That's the missing part in documentation. In plugins you need to expose a module, which will have defined plugin type, and i.e. factory function.

So first, register "xyz" in plugins using for example require function and path to your module, and then in frameworks just add "xyz" string.

This has been on the backlog for a long time! Doesn't seem like it's a difficult thing to fix up and it'd be handy as I found (was linked to) the documentation on inlining far before I found this issue...

I want to coment one thing. For framework inline plugin, factory function must not be arrow function, otherwise it gives error TypeError: Cannot read property '1' of null

:tada: This issue has been resolved in version 6.1.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

@zyf0330 thanks for mentioning it, I ran into the same issue and you helped me solve it!

Was this page helpful?
0 / 5 - 0 ratings