Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct inline plugin example? #1247

Closed
al-the-x opened this issue Nov 18, 2014 · 9 comments · Fixed by #3649
Closed

Correct inline plugin example? #1247

al-the-x opened this issue Nov 18, 2014 · 9 comments · Fixed by #3649

Comments

@al-the-x
Copy link

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.

@al-the-x
Copy link
Author

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

@rvowles
Copy link

rvowles commented Mar 11, 2015

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']
    });

@steveworkman
Copy link

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

@Hypnosphi
Copy link

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

@psmyrdek
Copy link

psmyrdek commented Jun 30, 2017

@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.

@FizzBuzz791
Copy link

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...

@zyf0330
Copy link

zyf0330 commented Feb 4, 2021

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

devoto13 added a commit to devoto13/karma that referenced this issue Feb 6, 2021
Changes:

- Promote `require('karma-plugin')` form over `'karma-plugin'` form. Former makes it more clear that plugin is imported from an NPM package and it is a regular JS object, there is no magic behind it. This is inspired by karma-runner#3498 where user is not aware that it is even possible. This also should make it easier with plug'n'play package managers (like Yarn 2).
- Explain that `plugins` array does not activate plugins, but only registers them to clarify karma-runner#1247 (comment).
- Explain the plugin structure, DI and how to build a new plugin.
- Re-arrange "Developing plugins" page to make it easier to add more information about every plugin type. Adding actual information should be done in the separate PRs though.

Fixes karma-runner#1247
devoto13 added a commit to devoto13/karma that referenced this issue Feb 12, 2021
Changes:

- Promote `require('karma-plugin')` form over `'karma-plugin'` form. Former makes it more clear that plugin is imported from an NPM package and it is a regular JS object, there is no magic behind it. This is inspired by karma-runner#3498 where user is not aware that it is even possible. This also should make it easier with plug'n'play package managers (like Yarn 2).
- Explain that `plugins` array does not activate plugins, but only registers them to clarify karma-runner#1247 (comment).
- Explain the plugin structure, DI and how to build a new plugin.
- Re-arrange "Developing plugins" page to make it easier to add more information about every plugin type. Adding actual information should be done in the separate PRs though.

Fixes karma-runner#1247
johnjbarton pushed a commit that referenced this issue Feb 12, 2021
Changes:

- Promote `require('karma-plugin')` form over `'karma-plugin'` form. Former makes it more clear that plugin is imported from an NPM package and it is a regular JS object, there is no magic behind it. This is inspired by #3498 where user is not aware that it is even possible. This also should make it easier with plug'n'play package managers (like Yarn 2).
- Explain that `plugins` array does not activate plugins, but only registers them to clarify #1247 (comment).
- Explain the plugin structure, DI and how to build a new plugin.
- Re-arrange "Developing plugins" page to make it easier to add more information about every plugin type. Adding actual information should be done in the separate PRs though.

Fixes #1247
@karmarunnerbot
Copy link
Member

🎉 This issue has been resolved in version 6.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@plusgut
Copy link

plusgut commented Apr 11, 2021

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

anthony-redFox pushed a commit to anthony-redFox/karma that referenced this issue May 16, 2023
Changes:

- Promote `require('karma-plugin')` form over `'karma-plugin'` form. Former makes it more clear that plugin is imported from an NPM package and it is a regular JS object, there is no magic behind it. This is inspired by karma-runner#3498 where user is not aware that it is even possible. This also should make it easier with plug'n'play package managers (like Yarn 2).
- Explain that `plugins` array does not activate plugins, but only registers them to clarify karma-runner#1247 (comment).
- Explain the plugin structure, DI and how to build a new plugin.
- Re-arrange "Developing plugins" page to make it easier to add more information about every plugin type. Adding actual information should be done in the separate PRs though.

Fixes karma-runner#1247
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants