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

Karma + Mocha + reporter options crash #3068

Closed
4 tasks done
Itee opened this issue Oct 13, 2017 · 5 comments
Closed
4 tasks done

Karma + Mocha + reporter options crash #3068

Itee opened this issue Oct 13, 2017 · 5 comments
Labels
invalid not something we need to work on, such as a non-reproducing issue or an external root cause

Comments

@Itee
Copy link

Itee commented Oct 13, 2017

Prerequisites

  • Checked that your issue isn't already filed by cross referencing issues with the common mistake label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
    node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

Mocha crash when setup option that are not prototyped (a.k.a function)
Error is: Uncaught TypeError: this[opt] is not a function at node_modules/mocha/mocha.js:135

Steps to Reproduce

Create a karma boilerplate and setup in karma.conf.js:

        // Pass configuration options directly to mocha
        client: {
            mocha: {
                reporter:        'myReporter',
                reporterOptions: { /*Ka-boom*/ }
            }
        }

Expected behavior: This to be fixed ?

Actual behavior: This crash the test run when set reporter options in karma.conf.js

Reproduces how often: 100%

Versions

Mocha version: 4.0.1
Node version: 8.7.0
System: Win10 x64
Shell: any
Browser: any
Third party: Karma v1.7.1
No transpiling

Additional Information

https://github.com/Itee/karma-mocha-reporterOption-crash
PR: #3069

@ScottFreeCode
Copy link
Contributor

Hi @ltee, could you elaborate on what you're trying to accomplish with a custom reporter in Karma?

If you want to customize the console output when running with Karma I believe you'd have to write a reporter for Karma rather than Mocha, and in that case Mocha's reporter options would not apply, unless I'm mistaken.

If there's something else you're trying to do instead (e.g. if there's a way to use Mocha's reporter to control what's on Karma's debug page?) I'd want to be sure that it works and isn't too hacky for Mocha to support before we change things to make it possible to use reporter options with it -- Mocha's browser reporter is pretty special-cased and I'm not familiar with how it fits with Karma.

@Itee
Copy link
Author

Itee commented Oct 16, 2017

Thank for reply.

First of all, i'm not talking about the reporter FOR karma ( like karma-mocha-reporter package ) but for Mocha reporters.

The goal is to use mocha reporter like mochawesome. But when i'm using mocha from karma the mocha reporter option are not correctly handled in mocha.

Form example using npm script:

  "scripts": {
    "mocha": "mocha test/my_app.unit.js --reporter mochawesome --reporter-options reportDir=./test/reports/,reportFilename=my_app.report"
  },

everything will be alright !

But when is use karma config file like this:

module.exports = function (config) {
  config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '../',

        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: [ 'mocha', 'chai' ],

        // list of files / patterns to load in the browser
    files: [
      'test/**/*.unit.js'
    ],

        // Pass configuration options directly to mocha
        client: {

            mocha: {
                reporter:        'mochawesome',
                reporterOptions: {
                    reportDir:       'test/reports/',
                    reportFilename:  'my_app.report',
                    reportTitle:     'My App Report'
                }
            }
        }

  })
}

The karmaconf.client.mocha.reporterOptions are not correctly handle by mocha.

In fact when using the npm script part, the reporter's options are send to mocha by Mocha constructor, while when using karma config ( which will use karma-mocha package ) the parameters will be handled by the setup methods. BUT, the setup method ( which is call in karma-mocha package in adapter.js at line 250 ) won't be able to set config properly. Because setup method expect that passed options are callable settings method.

here the setup methods:

/**
 * Setup mocha with the given setting options.
 */

mocha.setup = function (opts) {
  if (typeof opts === 'string') {
    opts = { ui: opts };
  }
  for (var opt in opts) {
    if (opts.hasOwnProperty(opt)) {
      this[opt](opts[opt]);
    }
  }
  return this;
};

I order to fix the usage of mocha reporter inside the karma package, you could: add the prototype that i had add. Or rewrite the setup methods to handle all potential mocha options.

I hope that i'm clearer. My request is not for karma reporter but for mocha reporter inside karma.

You could make a quick test using the repo: https://github.com/Itee/karma-mocha-reporterOption-crash

@ScottFreeCode
Copy link
Contributor

Hi @Itee,

I don't think that this is going to work, regardless of whether Mocha is changed; as far as I'm aware Mochawesome saves report files, and the Mocha instance running in Karma is running in the browser, where you can't save files. For instance, I believe if you run this without reporterOptions (just leaving Mochawesome with its default paths and names) Mochawesome will not save the reports (it might not be able to run at all).

Basically, it's not (directly) an issue of whether you want to use a Karma reporter or a Mocha reporter, it's an issue of whether you want to use a reporter that's meant to be run on the server or a reporter that's meant to be run in a browser. Mocha reporters are usually meant to be run on the server, so without being specifically designed for the browser I'm not sure any of them will work in Karma regardless of Mocha's setup code.

@boneskull boneskull added the type: question support question label Oct 17, 2017
@boneskull boneskull added invalid not something we need to work on, such as a non-reproducing issue or an external root cause and removed type: question support question labels Oct 17, 2017
@Itee
Copy link
Author

Itee commented Oct 17, 2017

Ok !
So it's a misunderstanding of my own...
I was thinking that karma was able to forward reports from open browser under testing.

So if i fully understand what you are saying, the only way to get full (html) report from reporters like mochawesome under karma running mocha, should be an package like 'karma-mocha-reporter' (but no for console) that handle karma output that come from mocha, and generate report on that output ?

@ScottFreeCode
Copy link
Contributor

Right -- that's definitely going to be the case for anything that writes files, and as far as I'm aware is the way it has to be done in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid not something we need to work on, such as a non-reproducing issue or an external root cause
Projects
None yet
Development

No branches or pull requests

3 participants